JVM structure study notes

JVM can be divided into 5 parts

1. Class Loader (Class Loader)

2. Runtime Data Area

3. Execution Engine (Execution Engine)

4. Native Interface (Native Interface)

5. Native Libraies

The most complicated of these is the runtime data area, which can be divided into method area, virtual machine stack, heap, and program counter. The method area and heap are shared by threads. The virtual machine stack, local method stack, and program counter are thread-isolated. Yes, the structure of JVM is shown in the figure below.
Insert picture description here
After understanding the structure of the JVM virtual machine, we will explain each part of it in detail.
Class loader: load bytecode files into memory

Execution engine: parse the JVM instruction, translate it into machine code, and submit it to the operating system after the analysis is complete.

Native library interface: a native library that integrates different development languages ​​for Java calls.

Local method library: the concrete realization of Java local method

Runtime data area: JVM core memory space structure model

The runtime data area is the most important part of the JVM memory structure. Next, we will explain in detail the various components of the runtime data area.

1. Method area

The method area stores class information, constants, and static variables loaded by the virtual machine. Data such as the code compiled by the just-in-time compiler. The method area is a norm. The permanent generation is an implementation of the method area. Here is a frequently tested interview question: the string constant pool of the previous version of jdk7 was placed in the permanent generation, jdk7 moved the string constant pool to the heap, and jdk8 directly deleted Permanent generation, use meta space instead of permanent generation.

2. Local method stack

The function and principle of the local method stack and the Java stack are the same, and both can be used to execute methods. The difference is that the Java stack executes Java methods, and the local method stack executes local methods.

3. Program timer

The program counter occupies a small memory space and is an indicator of the bytecode line number executed by the current thread. By changing the value of this counter, select the bytecode instructions that need to be executed. The program timers between multiple threads are independent of each other and do not affect each other, in order to ensure that each thread can find a specific location after recovery.

4.Java heap

The Java heap is used to store instantiated objects. The Java heap is shared by all threads. It is created when the virtual machine starts to store object instances. It is the bulk of the Java memory structure and takes up most of the space. It is the main management area of ​​the GC. It can be divided into young generation, old generation, permanent generation, jdk8 and later the permanent generation is removed.

5. Virtual machine stack

The memory model of Java method execution, multiple stack frames stored in the Java stack, each stack frame corresponds to a called method, mainly including local variable table, operand stack, dynamic connection, method return address. For the execution of each method, jvm will create a stack frame and push the stack frame onto the Java stack. After the method is executed, the stack frame is popped out.
Insert picture description here

Local variable table

Store all variables in the execution of the method, including the local variables and formal parameters declared in the method.

Operand stack

The calculation process in the method is based on the operand stack.

The specific operation method of the stack is like this, the compiler is implemented by two stacks, one is the stack that saves the operands, and the other is the stack that saves the operators. We traverse the expression from left to right, and when we encounter a number, we push it directly onto the operand stack. When encountering an operator, first compare it with the top element of the operator stack, if it is higher than the priority of the current stack top element, push it directly, otherwise take out the operator at the top of the current stack, and take out the first two of the operand stack at the same time The data is operated on, and the result is pushed into the operand stack. Repeat the above steps again until the current operator is pushed onto the stack. When no new operator needs to be pushed onto the stack, take out the current stack top element and the two operations of the operand stack, perform the operation, and push the result Operand stack, if the return value is required when the method is defined, just return the top element of the operand stack directly

Method return address

After a method call ends, it returns to the place where it was called, so the stack frame must keep the address that can return to the method call.

Guess you like

Origin blog.csdn.net/weixin_46011971/article/details/108906035
Recommended