Java virtual machine is mainly responsible for automatic memory management, class loading and execution, including execution engine, garbage collector, PC register, method area, heap area, direct memory, Java virtual machine stack, local method stack, and class loading subsystem. The method area and Java heap area are shared by all threads, while Java virtual machine stack, local method stack and PC register thread are private. The macro structure is shown in the figure below:
The JVM structure
Class loading subsystem
Load Class information from a file or network. Class information is stored in the method areaLoad -> Verify -> Prepare -> Parse -> Initialize -> Use -> UninstallSeveral stages, detailed process will be introduced in the subsequent article.
- loading
Class loaders can either use system-supplied loaders (the default) or customize class loaders by reading binary data for classes from a file or network, converting static storage structures represented by byte streams into method area runtime data structures, and generating Java object instances in the heap.
- The connection
The connection is divided into three stages: verification, preparation, and parsing. The verification stage ensures the correctness of class loading; preparation stage allocates memory for static variables of the class and initializes them to default values; and parsing stage converts symbolic references in the class to direct references
- Initialize the
The initialization phase is responsible for the initialization of a class. In Java, there are two ways to initialize a class variable. The initialization of a class is triggered only when the class is actively used, and the parent class is initialized first and then the child class is initialized.
- use
Class to access data structures in a method area. The object is data in the heap area
- uninstall
The program executes system.exit (), the program ends normally, and the JVM process terminates abnormally
Runtime data area
From static source code to compiled bytecode that the JVM execution engine can execute, the program goes through the class-loading process and creates logical run-time data areas in memory that are easy for the JVM to manage. The data areas are as follows, where garbage collection is automatically managed by the JVM
Stack frame data structure
The execution of a method in Java is performed in the virtual machine stack, which is private to each thread. Each method call and return the corresponding stack frame pushdown and unstack, where the stack frame holds the local variable table, method return address, operand stack and dynamic link information.
Dynamic link
During the execution of a method in Java, the stack frame holds symbolic references to the method, which are resolved into symbolic references through dynamic linking.
Memory management
- Memory partitioning (logically)
Java memory area can be logically divided into heap area and non-heap area. Before Java8, non-heap area was also called permanent area, and after Java8, it was unified into original data area. According to the generation model, the heap area can be divided into new generation and old age, among which the new generation is divided into Eden, SO and S1, so and S1 are two regions with the same size. In the production environment, you can adjust vm memory allocation ratio based on specific scenarios to achieve performance tuning.
Heap area is the biggest memory area of the JVM manages, Shared by all threads, using a generational model, heap area is mainly used for storing object instance, discrete space on pile can be physical, logical can be continuously, the heap memory size can be through the virtual machine parameter -xmx, – Xms specified, when the pile can’t continue to expand, the exception will be thrown OOM.
- Runtime instance
If the following SimpleHeap test class exists, the following figure shows the mapping between the heap area, Java stack, and method area of SimpleHeap in memory:
public class SimpleHeap {
/** * instance variable */
private int id;
/** * constructor **@param id
*/
public SimpleHeap(int id) {
this.id = id;
}
/** * instance method */
private void displayId(a) {
System.out.println("id:" + id);
}
public static void main(String[]args){
SimpleHeap heap1=new SimpleHeap(1);
SimpleHeap heap2=new SimpleHeap(2); heap1.displayId(); heap2.displayId(); }}Copy the code
Similarly, the Person class exists in the construction, so after the object instance is created, the relationship among the heap area, Java method stack and method area in memory is shown as follows:
Direct memory
The Java NIO library allows Java programs to use direct memory, which is an area of memory independent of the Java heap. Direct memory can be accessed faster than the heap. For performance reasons, direct memory can be operated directly for scenarios with frequent reads and writes. However, the sum of heap and direct memory must be less than system memory.
PC register
Thread private space, also known as program counter, at any time, a Java thread is always executing a method. The method being executed is called “current method”. If the current method is not a local method, the PC register points to the instruction being executed. If the current method is a local method, the VALUE of the PC register is undefined.
Garbage collection system
Garbage collection system is an important part of Java virtual machine. It mainly collects method area, heap area, and direct memory space. Unlike C/C++, garbage collection is implicit for all objects in Java, where garbage collection automatically completes memory management according to GC algorithm.