I recommend that if you want to see the relevant content of the virtual machine and have a good English foundation, you can directly read https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.6. 4
1. Runtime memory data area
According to the Java Virtual Machine Specification, the memory managed by the Java Virtual Machine includes the following runtime data areas:
Program counter, Java virtual machine stack, native method stack, Java heap, method area.
2. Introduction to the memory area managed by the virtual machine
2.1 Program Counter Register (PC Register): The program counter is private to the thread, that is to say, there is a program counter in each thread. In the conceptual model of the virtual machine, the bytecode parser works by changing the The value of the program counter is used to fetch the next bytecode instruction to execute.
If a Java method is executed, this counter records the address of the bytecode instruction being executed by the current thread; if it is a Native method (native method), the value of this counter is Undefined. This memory area is the only one in the Java virtual machine. There are no areas where any OutOfMemoryError conditions are specified in the specification.
2.2 Java Virtual Machine Stacks (Java Virtual Machine Stacks): The Java virtual machine stack is private to the thread, it is established with the establishment of the thread (the life cycle is the same as that of the thread).
The virtual machine stack describes the memory model of Java method execution: when each method is executed, a corresponding stack frame will be created in the stack. When the method call ends normally or abnormally, the stack frame is popped.
On the Java virtual machine stack, there are two types of exceptions:
If the stack depth requested by the thread exceeds the maximum stack depth (max_stacks), a StackOverflowError will be thrown.
If the virtual machine stack can be dynamically expanded, an OutOfMemoryError exception will be thrown when the expansion cannot apply for enough memory space.
Stack Frame: A stack frame is a data structure that supports method invocation and method execution in the virtual machine. It is the stack element of the Java virtual machine stack in the data area of the virtual machine during runtime.
The stack frame stores: the local variable table, the operand stack (also called the operation stack), the method return value (return Value), the reference to the runtime constant pool of the class to which the current method belongs, and some additional additional information. The size of the local variable table and the depth of the operand stack (max_stacks) are determined by the compiler and written into the Code attribute of the method table, current method --> current stack frame. All words run by the method execution engine Section code instructions only target the current stack frame.
2.2.1 Local variable table
2.2.1.1 The local variable table is the storage space for a set of variable values in the method. It stores the parameters of the method and the local variables inside the method. When the Java code is compiled into a Class file, the code of the method belongs to the max_locals data item. The maximum "capacity" of the local variable table that needs to be allocated to execute the method is determined in (compile-time allocation is completed). The capacity in the local variable is in Slot, and the virtual machine allows the length of the slot to vary with the operating system, virtual machine, processing It is different depending on the device, but as long as the appearance is guaranteed to look the same on a 64-bit virtual machine and a 32-bit virtual machine (using alignment padding). A slot can store a data type within 32 bits (boolean, byte, char, short, int, float, reference, returnAddress) 8 types. For 64-bit data types (long, double), the virtual machine allocates two consecutive Slot spaces in a high-order alignment. Since the local variable table is established in the thread's On the stack, it is thread-private data. No matter whether reading or writing two consecutive Slots is an atomic operation, there will be no data security problem. Because the virtual machine specification clearly stipulates that if you encounter a 64-bit data type in any way One of the two Slots in the class should throw an exception during the validation phase of class loading.
2.2.1.2 Use of the local variable table: When the method is executed, the virtual machine uses the local variable table to complete the value transfer process from the parameter value to the parameter variable table.
The virtual machine uses the "index positioning" method to use the local variable table, and the index value ranges from 0 to the maximum number of slots in the local variable table.
If an instance method is executed, the 0th Slot of the local variable table stores a reference to the object instance to which the method belongs (you can use this to access the current object), and the rest of the parameter assignments are arranged in parameter order, starting from 1, After the method parameter allocation is completed, the remaining Slots are allocated according to the variable order and scope defined inside the method body. In addition, Slots can be reused, if the bytecode instruction address recorded in the current program counter has exceeded the role of a variable domain, then the Slot corresponding to this variable can be handed over to other variables for use.
2.2.2 Operand stack
2.2.2.1 The operand stack is also called the operation stack (last-in, first-out), and the maximum "depth" of the operand stack (the stack refers to the depth, and the local variable table refers to the capacity) has been allocated in the compiler compilation stage. , and the Code attribute max_stacks of the write method will not exceed the maximum depth of the operation stack at any time when the method is executed. Among them, the 32-bit data type occupies the operation stack capacity of 1, and the 64-bit data type occupies the stack capacity 2. When the compiler compiles the program code, it must ensure that the data types of the elements in the operand stack "strictly" match the sequence of bytecode instructions, and this is also verified in the data flow analysis in the class verification phase.
2.2.2.2 Operand stack execution process: When a method starts to execute, the operand stack of this method is empty. During the execution of the method, various bytecode instructions will be written and written to the operand stack. Extract content (push and pop). For example, for int type addition (int a=98; int b=2; int c=a+b), first use iload_0 and
The iload_1 instruction pushes two int-type local variables (98 and 2) to the operand stack, and then executes the iadd addition instruction (pops the top two int operands from the operand stack, adds the calculation result, and then adds the calculation result Into the top of the operand stack), and finally use the istore_2 instruction to pop the int value at the top of the operand stack and store it in the second slot of the local variable table.
2.2.2.3 In the conceptual model of the Java virtual machine, the two stack frames are independent of each other. However, in actual situations, the implementation of some virtual machines can make the two stack frames overlap to achieve data sharing (let the following stack frames The operand stack is overlapped with the local variable table above, and no additional parameter copying is required. In addition, the interpretation execution engine of the Java virtual machine is called "stack-based execution engine", and the stack here is the operand stack. .
2.2.3 Stack Frame Information
2.2.3.1 Stack frame information: In actual development, we classify dynamic connections, method return values, and additional information into one category, called stack frame information.
2.2.3.1.1 Dynamic connection: Each stack frame has a reference to the runtime constant pool, which points to the runtime constant pool of the class to which the method being executed corresponding to the stack frame belongs. (This sentence is in zzm The in-depth understanding of the jvm inside the translation is wrong, the original English is The reference points to the constant pool for the class of the method being executed for that frame.)
This reference is held to support dynamic linking during method invocation. Dynamic linking is relative to "static analysis". The symbolic reference is converted to a direct reference during the class loading phase and when the symbolic reference is used for the first time The transformation is called "static resolution".
Dynamic linking converts symbolic references into direct references during program execution, which is called dynamic linking.
2.2.3.1.2 Method return value: There are two ways to return a method call, one is the normal method call completion (Normal Method Invocation Completion), the other is the abnormal call completion (Abrupt Method Invocation Completion, the English original meaning is sudden, abnormal blocking ).
For normal call return, if there is a return value, the method return value will be returned to the method caller (the return value and return type of the method are determined by the corresponding instruction, such as returning int type, the corresponding instruction is ireturn). When the normal call returns, The current stack frame needs to help restore the local variable table and operand stack of the caller's stack frame, and push the return value into
In the operand stack of the caller's stack frame, and adjust the value of the caller's program counter to point to the address of the next instruction after the method call is completed.
If the call is completed abnormally (such as an exception occurs inside the virtual machine, or an exception is explicitly thrown by the athrow instruction in the method call), if the exception is not caught by the current method, the method will exit without returning any value to the caller.
2.2.3.1.3 Additional information: The virtual machine specification allows some information not described in the specification to be added to the stack frame in a specific virtual machine implementation, such as debugging-related information. This information is called additional information.
2.3 Local method stack
In the Java virtual machine, the specific implementation of the native method stack is not specified (or may not be implemented). The Java virtual machine generally uses the C-linkage model to implement Java native calls (JNI), so we also call the native method stack "Cstacks". The native method stack is also thread-private and has the same life cycle as the thread.
It serves the native method (Native Method), and the native method (Native Method) can in turn call the Java method in the virtual machine. In this case, the thread will leave the native method stack and open a new one on the Java virtual machine stack. stack frame.
2.4 Non-heap memory
Non-heap memory includes Permanent Generation and Code Cache.
2.4.1 Permanent Generation contains two parts, one part is the method area (Method Area), the other part is the string constants (interned strings, interned strings, fixed strings -- string constants).
2.4.1.1 Method Area: The method area is created on virtual machine start-up, which stores information about each class, such as,
① Classloader Reference (Classloader Reference)
②Runtime constant pool (string constants, numeric constants, class references, member variable references, instance method references, etc.)
③ Member variable data (member variable name, type, modifier, attribute table): including instance variables and class variables (static, static variables)
④Member method data (method name, return value type, parameter type (in order), modifier, attribute table)
⑤ Method code: After being compiled into bytecode instructions by the compiler, it is stored in an attribute named Code in the method attribute table set (attributes). Code contains (bytecode, operand stack size, local variable size, Local variable table, exception table [start point, end point, program counter (PC) offset of exception handling code, constant pool index corresponding to the caught exception class]).
All threads share the same method area, so the process of accessing method area data and dynamic linking must be thread-safe. If two threads try to access a field or method of a class that has not yet been loaded, it must be loaded only once, and both threads must wait for it to be loaded before continuing.
2.4.1.2 Resident string (string constant): In HotSpot of JDK1.7, the string constant part has been removed from the method area.
2.4.2 The Code Cache is used to compile and store methods that are compiled into native machine code by the JIT compiler. Java bytecode is interpreted, but it is not as fast as executing machine code on the JVM's host CPU. To improve performance, Hotspot VM will find frequently executed hot codes and compile them into native code, which is stored off-heap in the code buffer in memory.
2.5 Heap Memory
The heap is used to allocate class instances, arrays at runtime. Data and objects cannot be allocated on the Java virtual machine stack, because stack frames are designed to not be resized after they are created, and stack frames only store references to objects or arrays in the heap.
Objects are always allocated on the heap and can only be reclaimed by the garbage collector. The heap is the key area managed by the garbage collector.
In order to support the garbage collection mechanism, the heap is divided into the following three areas: Young Generation (often divided into Eden and Survivor [From Survivor space and To Survivor space]), Old Generation, Permanent Generation Generation).