Article Directory
Content class file
java class files comprising bytecodes running, exactly as the data is arranged in the compact
class file binary stream, without any intermediate separator; at the beginning of the file is a special sign of a 0xcafebabe, baby coffee symbol (icon with the Java correspondence), for example, the following figure:
this file has complex formatting, is dedicated to the JVM to see, humans can use tools to read. There are versions access flag, constant pool, the current class, super class (parent class), interfaces, fields, methods, properties, and so on.
JVM learning
First, look at the situation JVM runtime data areas:
First, the java source of variation is the compiler into class bytecode, and then can see when the JVM is running into the thread section and thread exclusive shared portions of the two regions. Here is the main part of the adaptation of the JVM based on different operating systems, mainly as part of the above need to know.
Two concepts
- Thread Exclusive: Each thread will have its separate space occupied, with thread creation and destruction of life cycle
- Thread contributions: all threads can access this memory data, with the virtual machine creation and destruction or GC
Thus, we can know the method area and heap memory resources are shared can be summarized in different threads, and stack memory and the program counter thread resources are exclusive.
Thread shared area
Methods district
A method for storing area information is loaded classes, constants, variables static, compiled code, etc. things. 1.7 Method zone before it is called permanent behalf, called metadata space after 1.8.
Heap
Heap memory can be subdivided into the old era, the Cenozoic. Examples of mainly storing an object .. Garbage collection is the main GC heap memory management. If there will be full OutOfMemoryError, the follow-up model in memory I will continue to explain in detail.
Thread exclusive area
VM stack
VM stack is mainly stored in the stack frame, a thread of execution will be carried out one or more methods, this time on a stack frame corresponds to a method. Stack frame contents include:
- Local variable table
- Operand stack
- Dynamic Link
- Methods return address
- Additional information
The default stack memory size is 1M, if exceeded will report StackOverflowError; (as you write an infinite recursive method, certainly will be reported this wrong)
Native method stacks
And virtual machine stack almost, but put the local method (native modified), will exceed the size of the newspaper StackOverflowError;
Program Counter
Record the current thread executes bytecode position, bytecode instructions are stored in the address, executed if the Native method, the counter value is null. It occupies part of the space in the private space of each thread in only a small part. It is because the CPU at the same time will only execute one thread, it is necessary to record the position of execution so that when activated can be seamless.
JVM combat
Handle and a spout lip off, the specific operating principle depends on the JVM, but also by way of example, look at a piece of code:
public class Demo1 {
public static void main(String[] args) {
int x = 500;
int y = 100;
int a = x/y;
int b = 50;
System.out.println(a+b);
}
}
Generating a first class to compile bytecode javac:
javac Demo1.java
And then to browse for content by javap command:
javap -v Demo1.class>Demo1.txt #将生成的文件写到Demo1.txt上
Generated files:
Classfile /E:/wangyiyun/wangyiyun/src/Demo1.class
Last modified 2019-12-30; size 414 bytes
MD5 checksum ae6fa820973681b35609c75631cb255b
Compiled from "Demo1.java"
public class Demo1
minor version: 0 //次版本号
major version: 52 //主版本号 49对应5 50对应6 51->7 52->8
flags: ACC_PUBLIC, ACC_SUPER //访问标志
Constant pool:
#1 = Methodref #5.#14 // java/lang/Object."<init>":()V
#2 = Fieldref #15.#16 // java/lang/System.out:Ljava/io/PrintStream;
#3 = Methodref #17.#18 // java/io/PrintStream.println:(I)V
#4 = Class #19 // Demo1
#5 = Class #20 // java/lang/Object
#6 = Utf8 <init>
#7 = Utf8 ()V
#8 = Utf8 Code
#9 = Utf8 LineNumberTable
#10 = Utf8 main
#11 = Utf8 ([Ljava/lang/String;)V
#12 = Utf8 SourceFile
#13 = Utf8 Demo1.java
#14 = NameAndType #6:#7 // "<init>":()V
#15 = Class #21 // java/lang/System
#16 = NameAndType #22:#23 // out:Ljava/io/PrintStream;
#17 = Class #24 // java/io/PrintStream
#18 = NameAndType #25:#26 // println:(I)V
#19 = Utf8 Demo1
#20 = Utf8 java/lang/Object
#21 = Utf8 java/lang/System
#22 = Utf8 out
#23 = Utf8 Ljava/io/PrintStream;
#24 = Utf8 java/io/PrintStream
#25 = Utf8 println
#26 = Utf8 (I)V
{
public Demo1();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=3, locals=5, args_size=1
0: sipush 500
3: istore_1
4: bipush 100
6: istore_2
7: iload_1
8: iload_2
9: idiv
10: istore_3
11: bipush 50
13: istore 4
15: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
18: iload_3
19: iload 4
21: iadd
22: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
25: return
LineNumberTable:
line 3: 0
line 4: 4
line 5: 7
line 6: 11
line 7: 15
line 8: 25
}
SourceFile: "Demo1.java"
Basic Information
minor version: 0 //次版本号
major version: 52 //主版本号 49对应5 50对应6 51->7 52->8
flags: ACC_PUBLIC, ACC_SUPER //访问标志
The three above corresponds to the version information and access flag, to reflect the basic information, the following is an access flag table:
Constant pool
Constant pool correspondence is static information after class compiler can compile confirmation.
Constructor
public Demo1();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
Because there is no definition of a constructor, use the default constructor with no arguments