Analysis of virtual machine class loading mechanism (1)

A class starts from being loaded into the memory of the virtual machine and ends when it is unloaded from the memory. Its entire life cycle includes seven stages of loading, verification, preparation, parsing, initialization, use, and unloading. Verification, preparation, and parsing are collectively referred to as connection. The sequence of occurrence of the 7 stages is shown in the figure

1. Loading

There are three things a virtual machine needs to do during the loading phase

(1) Obtain the binary byte stream that defines this class through the fully qualified name of a class.

(2) Convert the static storage structure represented by this byte stream into the runtime data structure of the method area.

(3) An object of java.lang.Class representing this class is generated in memory as the access entry for various data of this class in the method area.

2. Verification

The purpose of verification is to ensure that the information contained in the byte stream of the Class file meets the requirements of the current virtual machine and does not endanger the security of the virtual machine itself.

The verification mainly completes the following four inspection actions

(1) File format verification: Verify whether the byte stream conforms to the specification of the Class file format and can be processed by the current version of the virtual machine.

(2) Metadata verification: Semantic analysis is performed on the information described by the bytecode to ensure that the information described meets the requirements of the Java language specification.

(3) Bytecode verification: Verify and analyze the method body of the class to ensure that the method of the verified class will not cause events that endanger the security of the virtual machine during runtime.

(4) Compliance reference verification: This verification occurs when the virtual machine converts a symbolic reference to a direct reference, that is, the parsing phase. This verification can be seen as a matching check for information other than the class itself (various symbolic references in the constant pool).

3. Preparation

The preparation phase is the phase of formally allocating memory for class variables and setting the initial values ​​of class variables. The memory used by these variables will be allocated in the method area. At this time, only class variables are allocated for memory allocation, not instance variables. Instance variables will be allocated in the Java heap along with the object when the object is instantiated. Typically, the initial value refers to the zero value of the data type.

Suppose a class variable is defined as:

public static int value=111;

Then the initial value of the variable value after the preparation phase is 0 instead of 111, because no java method has been executed yet, and the putstatic instruction that assigns the value to 111 is stored in the class constructor <clinit>( ) method, so the action of assigning value to 111 will not be executed until the initialization phase.

In addition, in special cases, if the ConstantValue attribute exists in the field attribute table of the class field, the variable value will be initialized to the value specified by the ConstantValue attribute during the preparation phase. E.g

public static final int value=111;

When compiling, Javac will generate the ConstantValue property for the value. In the preparation stage, the virtual machine will assign the value to 111 according to the setting of ConstantValue.

4. Analysis

The parsing phase is the process by which the virtual machine replaces symbolic references in the constant pool with direct references

Association of Symbolic References and Direct References

(1) Symbolic references have nothing to do with the layout implemented by the virtual machine, and the referenced target does not necessarily have to be loaded into memory. The memory layout of various virtual machine implementations can be different, but the symbolic references they can accept must be consistent, because the literal form of symbolic references is clearly defined in the Java virtual machine specification's Class file format.

(2) The direct reference can be a pointer to the target, a relative offset or a handle that can be located indirectly to the target. If there is a direct reference, the target of the reference must already exist in memory.

The parsing action is mainly performed for 7 types of symbol references such as class or interface, field, class method, interface method, method type, method handle and call point qualifier, corresponding to CONSTANT_Class_info, CONSTANT_Fieldref_info, CONSTANT_Methodref_info, CONSTANT_InterfaceMethodref_info, CONSTANT_MethodType_info, CONSTANT_MethodHandle_info and CONSTANT_InvokeDynamic_info7 of the constant pool respectively. Medium constant type.

5. Initialization

The initialization phase is the process of executing the class constructor <clinit>() method. The <clinit>() method is merged by the compiler automatically collects the assignment operation of the class variable in the class and the statement in the static statement block. The virtual machine ensures that the <clinit>() method of the parent class has been executed before the <clinit>() method is executed. If there is no static variable assignment or static statement block in a class, the compiler may not generate the <clinit>() method for this class.

There are only 5 situations in which the class must be initialized immediately:

(1) When encountering the four bytecode instructions of new, getstatic, putstatic, or invokestatic, if the class has not been initialized, its initialization needs to be triggered first.

(2) When using the method of the java.lang.reflect package to make a reflection call to a class, if the class has not been initialized, its initialization needs to be triggered first.

(3) When initializing a class, if you find that the parent class has not been initialized, you need to trigger the initialization of its parent class first.

(4) When the virtual machine starts, the user needs to specify a main class to be executed, and the virtual machine initializes this main class first.

(5) When using the dynamic language support of JDK1.7, if the last parsing result of a java.lang.invoke.MethodHandle instance is the method handle of REF_getStatic, REF_putStatic, and REF_invokeStatic, and the class corresponding to this method handle is not initialized, then Its initialization needs to be triggered first.

-------------------------------------------------- ----- Quoted from "In-depth understanding of JVM virtual machine"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162007&siteId=291194637