Class loading process description-java

In Java, a variable has a process from creation, use to destruction. There must be a similar life cycle for the creation, use, and destruction process of a class. The life cycle of a class in java generally includes such several parts: the
Insert picture description here
class loading phase is only the first five phases: loading, verification, preparation, parsing, and initialization , but the position of the parsing phase is not fixed, because there is still dynamic binding in java .

What is the class loading mechanism of java?
The Java virtual machine loads the data describing the class from the Class file into the memory, and performs verification, conversion, analysis and initialization of the data, and finally forms a Java type that can be directly used by the virtual machine. This process is called class loading of the virtual machine. mechanism.

1. Loading phase:

1) Obtain the binary byte stream that defines this class through the fully qualified name of a class. (The file can be obtained from any place, it can be generated from ZIP archive, network, runtime calculation, and other files)
2) Convert the static storage structure represented by this byte stream into the runtime data structure of the method area . (This is the process of loading from external memory to memory)
3) Generate a java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area.

At this stage, it includes the class file generated after the java file is compiled. In addition to the class version, field, method, interface and other information, the class file also has a constant pool.

The loading phase can also be partially involved in the way of user-defined class loader .

2. Verification phase:

The purpose of this stage of verification is to ensure that the information contained in the byte stream of the Class file meets all the constraint requirements of the "Java Virtual Machine Specification", and to ensure that this information will not endanger the security of the virtual machine itself after being run as code. The main thing is to ensure compliance and safety inspections.

These include file format verification, metadata verification, bytecode verification, and symbol reference verification.

3. Preparation stage:

The preparation stage is the stage of formally allocating memory for the variables defined in the class (that is, static variables, variables modified by static) and setting the initial value of the class variable.

Note that: this time for memory allocation includes only class variables, and does not include the instance variables, instance variables will be allocated in the Java heap together with the object when the object is instantiated. Followed here is the initial value of "general" is the default value for the data type, for example: public static int value = 123; the preparation phase, but will not directly value is set to the value of 123, but values 0 , Because at this time no Java method has been executed yet, and the putstatic instruction that assigns value to 123 is stored in the class constructor () method after the program is compiled, so the action of assigning value to 123 involves the initialization of the class The stage will be executed.

4. Analysis stage:

The parsing stage is a process in which the Java virtual machine replaces symbol references in the constant pool with direct references .

Symbol reference : A symbol reference describes the referenced target by a set of symbols. The symbol can be any form of literal, as long as it can be used to locate the target without ambiguity. The symbolic reference has nothing to do with the memory layout implemented by the virtual machine, and the target of the reference is not necessarily the content that has been loaded into the virtual machine's memory.

Direct reference : A direct reference is a pointer that can directly point to the target, a relative offset, or a handle that can indirectly locate the target. Direct references are directly related to the memory layout implemented by the virtual machine. The direct references translated from the same symbol reference on different virtual machine instances are generally not the same. If there is a direct reference, the referenced target must already exist in the virtual machine's memory.

5. Initialization phase:

The initialization phase is the process of executing the class constructor <clinit>() method . <clinit>() is not a method written by programmers directly in Java code, it is an automatic generation of Javac compiler.

The <clinit>() method is generated by the compiler automatically collects the assignment actions of all class variables in the class and the statements in the static statement block (static{} block). The compiler collects the order by the statement in the source file The order of appearance determines that in a static statement block, only variables defined before the static statement block can be accessed, and variables defined after it can be assigned but cannot be accessed in the previous static statement block.

Note: The
Java virtual machine must ensure that the <clinit>() method of a class is correctly locked and synchronized in a multithreaded environment. If multiple threads initialize a class at the same time, only one of the threads will execute the class <clinit> () method, other threads need to block and wait until the active thread finishes executing the <clinit> () method.

Guess you like

Origin blog.csdn.net/m0_46551861/article/details/115180288