Summary of JVM class loading mechanism

The loading mechanism of class is divided into the following three stages: loading, connecting and initialization. The connection is divided into three small stages: verification, preparation and parsing.

Loading phase

The of the class The binary data in the class file is read into memory and placed in the method area of the runtime data area, and then a class object is created in the heap to encapsulate the data structure of the class in the method area. How to load class files: directly load from the local system and download through the network Class files are loaded from zip, jar and other archive files The class file is extracted from a proprietary database Class file dynamically compiles java source files into Class file

Verification phase

The verification contents in this stage are as follows: check the structure of the class file: ensure that the class file complies with the fixed header format of the Java class file, just like the file upload verification file header at ordinary times. It also verifies the major and minor version numbers of the file to ensure that the version number of the current class file is compatible with the current JVM. Verify whether the byte stream of the class is complete, and verify according to the MD5 code. Semantic check: check whether the class has a parent class, whether the parent class is legal and exists. Check whether the class is final and inherited. Classes modified by final are not allowed to be inherited. Check whether the method overload of this class is legal. Check whether the byte code stream translated by the class method is legal. Reference validation to verify whether other classes and methods used by the current class can be found successfully.

Preparation stage

After passing the validation phase, start to allocate memory to the static variables of the class and set the default initial value. The memory of class variables will be allocated to the method area, and instance variables will be allocated to heap memory. Variables in the preparation stage will be given initial values, but those of final type will be given their values. It can be understood that when compiling, they will be directly compiled into constants and assigned to. If it is a variable of type int, it will be allocated 4 bytes of memory space and given a value of 0. If it is long, it will be given 8 bytes and 0.

Analysis stage

The parsing phase replaces symbolic references in the class with direct references. For example, the gotowork method of the worker class will reference the run method of the car class.

Initialization phase

The initialization phase of a class is to assign correct values to all variables in the base. The assignment of static variables and member variables are completed here. For the order of initialization, refer to the collation above. There are several points to note in initialization. If the class has not been loaded and connected, load and connect it first. If there is a direct parent class and the parent class is not initialized, initialize the parent class first.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>