Introduction to jvm memory model

94f07bc01e4546cd97adeecfc74768fc.gif1. Class loading subsystem

 

1. The process of class loading: loading, linking, and initialization. Linking is divided into verification, preparation, and parsing.

Load: Load class files

 

Verification: Ensure that the information contained in the byte stream meets the current virtual machine requirements

 

Preparation: allocate memory and set initial values

 

Parsing: Change the variable from a symbolic reference to a direct reference

 

Initialization: Execute class constructor method

 

 

 

2. Classification of class loaders

Class loaders are used in the loading phase. Class loaders are divided into: bootstrap class loaders implemented by C++ and custom loaders implemented by Java.

 

Custom loaders are divided into three levels of class loaders: extension class loaders, application class loaders and system class loaders.

 

 

 

①. Boot class loader: Start the class loader and load the class library recognized by the Java virtual machine stored in the lib directory or in the path specified by the -Xbootclasspasth parameter into the virtual machine;

 

②. Expand the class loader: load all class libraries in the \lib\ext directory or the path specified by the java.ext.dirs system variable into the virtual machine;

 

③. System class loader: Application class loader: Responsible for loading all class libraries on the user class path. This class loader can be used directly in development;

 

④. Custom class loader: user-defined classes.

 

3. Parental delegation mechanism

The Java virtual machine adopts an on-demand loading method for class files, that is, only when the class is needed, the corresponding class is loaded into the memory, and the parent delegation mechanism is used for loading:

 

①: When a class loader receives a class loading request, it will not load it immediately, but will delegate the request to the parent class loader for loading;

 

②. When the parent class loader still has a parent class loader, continue to delegate in order, and finally delegate to the top-level startup class loader;

 

③. If the parent class loader can complete the class loader, it will return successfully; if the parent class loader cannot complete the loading, the child class loader will try to load it by itself.

 

This is the loading process of the parental delegation mechanism.

 

Advantages of the parent delegation mechanism:

Can avoid repeated loading of classes;

 

Protect program security and prevent core APIs from being tampered with.

 

4. Sandbox security mechanism

Java code is limited to a specific operating range of the virtual machine, and the code's access to local resources is strictly restricted. This protects the effective isolation of the code and prevents damage to the local system. This is the sandbox security mechanism.

 

The sandbox mainly restricts access to system resources, such as CPU, memory, file system, and network. Different levels of code may also have different restrictions on access to these resources.

 

2. Runtime data area

1. Virtual machine stack

When each thread is created, a virtual machine stack is created, and the virtual machine stack is private to the thread.

 

 

 

a、栈帧(Stack Frame)

The virtual machine stack stores stack frames one by one, and each stack frame corresponds to a method, which is a data structure used to support method invocation and method execution;

 

The virtual machine stack frame stores the local variable table, operand stack, dynamic link and method return address of the method. When compiling the code, the size of the local variable table and the depth of the operand stack required in the stack frame have been determined. .

 

①Local variable table: It is a set of variable value storage space used to store method parameters and local variables defined within the method. Note: In the main() method, the parameter at the first position in the first table is args;

 

The local variable table stores various basic data types, object references and method return addresses known to the compiler;

 

The local variable table uses variable slots as the smallest unit. Each slot can store a length of 32 bits. That is, long type and double type occupy two variable slots, while other basic data types occupy one variable slot.

 

② Operand stack: used to save intermediate results during the calculation process, and also serves as a temporary storage space for variables during the calculation process;

 

③Dynamic link: A reference pointing to the method to which the frame belongs in the runtime constant pool; the purpose of including this reference is to support the code of the current method to implement dynamic linking, such as the invokedynamic instruction.

 

When the Java source file is compiled into a byte file, all variables and method references are saved in the constant pool of the class file as symbolic references, and are loaded into the runtime constant pool of the method area when the program is running;

 

For example, when a method is described as calling another method, it is represented by a symbolic reference pointing to the method in the constant pool. Then the role of dynamic linking is to convert these symbolic references into direct references to the calling method.

 

④Method return address: Store the value of the method in the register, which is the instruction address of the method. It is convenient for the execution engine to return to the instruction line number corresponding to the method after executing the method, so that execution can continue.

 

2. Program counter

The program counter, also known as the pc register, stores the address of the next instruction to be executed. Each thread has its own pc register.

 

When the program is executed, different threads will be continuously switched for execution. Some threads may be executed normally and then be stopped and switched to other threads. At this time, the pc register will record the known instruction line number and wait until the thread obtains it. When the resource is re-executed, it will continue to execute according to the instructions in the pc register.

 

3. pile

 

 

The heap consists of the new generation and the old generation

 

new generation

The new generation consists of the Eden area and two Survior areas, Survior0 and Survior1. The new generation is generally used to store newly created objects.

 

①Endn area: Almost all objects are created in the Eden area, that is, Eden is where most objects are generated;

 

Some of the created objects have a short existence period, and some objects have a very long existence period. When the memory in the Eden area is full, Minor GC will be triggered for garbage collection;

 

②Survior area: Survivor area. The survivor area is divided into two areas, Survior0 and Surivor1, that is, Survivor area 0 and Survivor area 1. During garbage collection, whichever area is empty is Survior0; generally the two areas are replaced with each other to become Survior0. ;

 

The storage space of Survior0 and Survior1 is generally at a ratio of 1:8, but in most cases this ratio is not reached. You can use the -XX:SurviorRatio parameter to set it.

 

old age

The old generation is generally used to store objects with long life cycles. When the storage space in the old generation is insufficient, Major GC or Full GC will be triggered for garbage collection.

 

The storage space ratio between the new generation and the old generation is 1:2, which can be set through the -XX:NewRatio parameter.

 

garbage collection

In the heap, there are three garbage collection algorithms: Minor GC, Major GC and Full GC

 

①Minor GC

New generation garbage collection is only garbage collection of the new generation (Eden, Survior0 and Survior1).

 

Trigger mechanism: Triggered when there is insufficient space in the Eden area in the new generation (note: it will not be actively triggered when there is insufficient space in the Survior area, and the Survior area will only passively trigger the mechanism) for garbage collection. Because most Java objects have the characteristics of ephemeral life and death, Minor GC is triggered very frequently, and the recovery speed is generally relatively fast.

 

Collection process: a. In the new generation, there is a reference counter for each object. When the space in the Eden area is full, only the objects that are still referenced can survive and will not be cleaned up, and will be transferred to the Survior0 area. The reference of the object Increasing the counter by 1 means that the object's surviving age increases by one year;

 

b. Repeat step a to copy the surviving objects to the S0 area. The survival age increases, and the survival age of the objects that already exist in the S0 area also increases accordingly;

 

c. When the Eden area and S0 area are full, the objects in S0 will be transferred to S1, and the surviving objects will have corresponding survival ages. Then the Eden and S0 areas will be cleared, and all surviving objects will be in S1, and Objects have different survival ages. At this time, S0 will become S1 in the sense (the area is empty), and S1 will become S0 in the sense; and so on, the empty area is S1, and the data area where the object survives is S0.

 

d. When the Minor GC is continuously performed until the object's survival year reaches the threshold (set by -XX: MaxTenuringThreshold, the default is 15), the object that reaches the threshold age will be judged as an object with a longer life cycle and will be promoted to the old generation. ;Continuously performing Minor GC, objects will continue to be promoted to the old generation.

 

TLAB: TLAB is a storage space set up to solve thread safety issues. It belongs to the Ede area of ​​the new generation. The TLAB memory space is very small, accounting for only 1% of Eden. Not all object instances can successfully allocate memory in TLAB. , but the JVM does use TLAB as its first choice for allocating memory.

 

The heap space is shared by all threads, but TLAB in Eden is thread-private, solving thread safety issues.

 

Major GC

Old generation garbage collection, only garbage collection in the old generation is performed.

If an object is very large, it will directly enter the old generation. That is, there are large objects and long-lived objects in the old generation.

 

Trigger mechanism: When there is insufficient space in the old generation, it will try to trigger the Minor GC to collect garbage in the new generation. If the space is still insufficient after the trigger, the Major GC will be triggered;

 

The speed of Major GC is generally more than 10 times slower than that of Minor GC, and the STW time is longer. If the space is still insufficient after triggering, an OOm error will be reported.

 

Only CMS GC will collect the old generation separately. Many times, Major GC and Full GC are confused and triggered.

 

Collection process: Generally, Major GC and Full GC are triggered at the same time, collecting the entire heap.

 

Full GC

Whole heap collection collects garbage from the entire Java heap and method area.

 

Trigger mechanism: a. When calling System.gc(), the system will recommend executing Full GC, but it is not necessarily executed;

 

b. When there is insufficient space in the old generation;

 

c. When there is insufficient space in the method area;

 

d. The average size of the old generation after passing Minor GC is greater than the available memory of the old generation;

 

e. When copying from Eden area and Survior space0 (From space) area to Survior space (To space) area, the object size is larger than the available space size of To space, then the object is copied to the old generation, but the object size is larger than the available space of the old generation. triggered when.

 

Full GC should be avoided as much as possible during development or tuning, so that the pause time will be shorter.

 

Code optimization

The heap is not the only option for allocating object storage. If it is found through escape analysis that an object does not have an escape method, it may be optimized to allocate on the stack.

 

Escape analysis: Analyze the scope of an object. When an object is determined in a method, and the object is only used inside the method, it is considered that no escape has occurred; when an object is determined, it is used by an external method (for example: a variable is returned or passed as a parameter to other methods), an escape is considered to have occurred.

 

Allocation on the stack: Objects that have not escaped can be allocated on the thread-specific stack. As the method execution ends, the stack space is also removed, and the variables are also removed, without the need for GC.

 

Code optimization (no escape analysis occurs)

1. Allocation on the stack;

 

2. Synchronization omitted: Only one thread accesses the variable, canceling its synchronization strategy;

 

3. Separate objects or scalar replacement

 

Scalar: refers to a data that cannot be broken down into smaller data, such as a basic data type.

 

Split object or scalar replacement is to break a large object into multiple scalars and store them at non-contiguous memory addresses.

 

4. Method area--Metaspce metaspace

The method area is the same as Java. It is an area shared by each thread. It is used to store information about loaded classes, constants, static variables, even compiled code and other data.

 

The evolution of metaspace

 

 

5. Local method stack

The java virtual machine stack is used to manage the invocation of java methods, and the local method stack is used to manage the invocation of local methods.

 

Native method: Java calls the interface of non-Java code, such as the operating system or exchanging information with some hardware.

 

Java applications interact with the environment outside Java, which is why native methods exist.

 

When a thread calls a native method, it enters a new world that is no longer restricted by the Java virtual machine. It has the same permissions as the virtual machine.

Guess you like

Origin blog.csdn.net/weixin_57763462/article/details/132927276