Home >Java >javaTutorial >Detailed graphic explanation of the process of JVM loading a class

Detailed graphic explanation of the process of JVM loading a class

黄舟
黄舟Original
2017-03-08 11:08:091326browse

This article mainly introduces the process of JVM loading a class. It has a very good reference value. Let’s take a look at it with the editor below

The class loading process

Java source code is compiled into class bytecode, and the JVM loads the bytecode .Class file that describes the class data. into the memory, and performs verification, conversion, analysis and initialization on the data, and finally forms a Java type that can be directly used by the virtual machine. This is the class loading mechanism of the virtual machine.

From the time a class is loaded into the virtual machine memory to the time it is unloaded from the memory, its life cycle includes: Loading, Verification, Preparation, Resolution, and Initialization. , using (Using) and unloading (Unloading) seven stages, of which the three parts of verification, preparation, and parsing are collectively referred to as links.

The order of the five stages of loading (loading), verification, preparation, initialization and unloading is fixed. The loading process of the class must start in this order, while the parsing stage does not necessarily; it can in some cases Start after initialization, this is for the runtime dynamic binding feature (also called dynamic binding or late binding, such as overriding).

1. Loading:

During the loading phase, the virtual machine mainly completes three things:

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. Generate a java.lang.Class object representing this class in the Java heap as an access entrance to the method area data

Compared to other stages of the class loading process, the loading stage (preparatory ground) Said to be the action of obtaining the binary byte stream of the class in the loading phase) is the most controllable phase in the development period, because the loading phase can be completed using the class loader (ClassLoader) provided by the system, or it can be customized by the user. The class loader is completed, and developers can control how the byte stream is obtained by defining their own class loader.

After the loading phase is completed, the binary byte stream outside the virtual machine is stored in the method area according to the format required by the virtual machine. The data storage format in the method area is defined by the virtual machine implementation, and the virtual machine does not stipulate this. The specific data structure of the region. Then an object of the java.lang.Class class is instantiated in the java heap. This object serves as the external interface for the program to access these types of data in the method area.

2. Verification:

The purpose of the verification phase is to ensure that the information contained in the byte stream of the Class file complies with the JVM specifications and will not cause harm to the JVM. If verification fails, a java.lang.VerifyError exception or its subclass exception will be thrown. The verification process is divided into four stages

1. File format verification: Verify whether the byte stream file complies with the Class file format specifications and can be processed correctly by the current virtual machine.

2. Metadata verification: It is a semantic analysis of the information described by the bytecode to ensure that the information described conforms to the specifications of the Java language.

3. Bytecode verification: It mainly analyzes the data flow and control flow to ensure that the methods of the verified class will not harm the virtual machine when running.

4. Symbol reference verification: Symbol reference verification occurs when the virtual machine converts a symbol reference into a direct reference. This conversion action will occur during the parsing phase.

3. Preparation:

The preparation phase allocates memory for variables and sets the initialization of class variables. At this stage, only class variables (static modified variables) are allocated, not class instance variables. For non-final variables, the JVM will set it to "zero value" instead of the value of its assignment statement:

pirvate static int size = 12;

Then at this stage, the value of size is 0, not 12. Class variables modified by final will be assigned real values.

4. Parsing:

The parsing phase is the process of replacing symbol references in the virtual machine constant pool with direct references.

Symbolic reference: A symbolic reference is a set of symbols to describe the referenced target object. The symbol can be any form of literal, as long as the target can be located unambiguously when used. Symbolic references have nothing to do with the memory layout implemented by the virtual machine, and the referenced target object does not necessarily have to be loaded into memory.

Direct reference: A direct reference can be a pointer directly pointing to the target object, a relative offset, or a handle that can indirectly locate the target. Direct references are related to the implementation of virtual machine memory layout. 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 reference target must already exist in the memory.

The virtual machine specification does not stipulate the specific time when the parsing phase occurs. It only requires 13 operating symbol references during the execution of anewarry, checkcast, getfield, instanceof, invokeinterface, invokespecial, invokestatic, invokevirtual, multianewarray, new, putfield and putstatic. Before the bytecode instructions, the symbol references they use are parsed first, so the virtual machine implementation will judge as needed whether to parse the symbol references in the constant pool when the class is loaded by the loader, or wait until a A symbolic reference is resolved just before it is used.

The parsing action is mainly performed on four types of symbol references: classes or interfaces, fields, class methods, and interface methods. Corresponds to the four constant types CONSTANT_Class_Info, CONSTANT_Fieldref_Info, CONSTANT_Methodef_Info, and CONSTANT_InterfaceMethoder_Info in the compiled constant pool respectively.

1. Class and interface analysis

2. Field analysis

3. Class method analysis

4.Interface method analysis

5. Initialization:

The initialization phase of a class is the last step in the class loading process. In the preparation phase, the class variables have been assigned the initial value required by the system. In the initialization phase, it is based on the subjective plan made by the programmer through the program. Initialize class variables and other resources, or it can be expressed from another perspective: the initialization phase is to execute the class constructor

6. Use:

new thread---program counter----jvm stack execution (object reference )-----Heap memory (direct reference)----Method area

7. Uninstall:

GC garbage collection


The above is the detailed content of Detailed graphic explanation of the process of JVM loading a class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn