JVM knowledge point - class loading process

1. Java class loading goes through seven processes

load

  The first process of loading when a class is loaded, at this stage, three things are done:

  1) Get the binary stream of a class by its fully qualified name

  2) Convert the static storage structure in the binary into the runtime data result of the method area

  3) Generate the Class object of the class in memory as the data access entry of the class

verify

 The purpose of verification is to ensure that the information in the byte stream of the Class file will not endanger the virtual machine. The following four verifications are mainly completed at this stage

1) File format verification: Verify whether the byte stream conforms to the specifications of the Class file, such as whether the major and minor version numbers are within the scope of the current virtual machine, and whether the constants in the constant pool have unsupported types

2) Metadata verification: Semantic analysis of the information described by the bytecode, such as whether this class has a parent class, whether it integrates classes that are not inherited, etc.

3) Bytecode verification: It is the most complex stage of the entire verification phase. By verifying the analysis of data flow and control retention, it is determined whether the program semantics is correct, mainly for the verification of the method body. For example, whether the type conversion verification in the method is correct, whether the jump instruction is correct, etc.

4) Symbolic reference verification: This action occurs in the subsequent parsing mainly to ensure that the parsing action can be executed correctly.

Prepare

The preparation phase is to allocate memory for the static variables of the class and initialize them to default values, which will be allocated in the method area. The memory of the instance variables in the class is not allocated during the preparation phase. The instance variables will be allocated in the java heap along with the object when the object is instantiated.

public static int value=123;//In the preparation stage, the initial value of value is 0. It will only become 123 during the initialization phase.
 Parse

 This stage mainly completes the conversion of symbolic references to direct references. The parsing action is not necessarily before the initialization action is completed, it may also be after the initialization is completed

initialization

 Initialization is the last step of class loading. In the previous class loading process, in addition to the user-defined class loader that can participate in class loading, the rest are completed by virtual machine calls, and only the initialization phase is the user's real participation in java code initialization.

 

In summary, the virtual machine loads the data of the class to be described into the memory from the Class file, prepares for parsing through verification, completes the initialization, and finally forms the java type that is directly used by the virtual machine.

 

Second, the parent delegation loading mechanism of class loading

 When a class loader wants to load a class, the class loader does not load the class first, but delegates loading to its parent class loader. When the parent class loader cannot load, the class loader loads the class. kind.

Third, what is a class loader, and what are the class loaders?

 There are four main class loaders

 1. Start the class loader, which is used to load the java core class library, which cannot be directly used by java programmers

 2. The extension class loader, which is used to load the extension classes of java. The implementation of the java virtual machine will provide an extension directory. The class loader finds and loads java classes in this directory

 3. The system class loader, which loads the java class according to the classpath of the java application. Generally speaking, the program of the java application is loaded through it, and it can be obtained through ClassLoader.getSystemClassLoader().

 4. User-defined loader, completed by inheriting the java.lang.Classloader class

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326851372&siteId=291194637