Deep understanding of Java Virtual Machine-Chapter 7: deep understanding of Java Virtual Machine
Chapter 2 Virtual Machine Loading Mechanism
Class loading time
Load Loading, connect Linking (verify Verfiication, prepare Preparation, parse Resolution) to initialize Initialization, use Using and Unloading
Class loading process:
1. Load:
Loading is a single stage in the "class loading" class loading process. In the loading phase, the virtual machine must complete the following three tasks.
1. Get the binary byte stream that defines this class using the full 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 that represents this Class in the memory, which serves as the access portal for various data of this Class in the method area.
2. Verification
1. File Format verification: verify whether the byte stream complies with the specification of the Class file format and can be processed by the current version of the virtual machine.
2. Metadata verification: Perform Semantic Analysis on the bytecode description. Whether this class has a parent class and whether it inherits a class that is not allowed to be inherited ....
3-byte code verification: The main purpose is to determine that the program semantics is legal and logical through data flow and control flow analysis. After the data type in the metadata information is verified in the second stage, the method body of the class will be verified and analyzed, ensure that the methods of the verified class do not cause events that endanger the security of virtual machines at runtime. Ensure that the data type of the operand stack can work with the command code sequence at any time. For example, if this is not the case, an int type data is placed in the Operation stack, during use, the local variable table is loaded according to the long type.
4. Verification of the last phase of symbolic reference verification occurs when the Virtual Machine converts the symbolic reference to a direct reference. This conversion action will take place in the third phase of the connection-the parsing phase. Symbol reference verification can be seen as matching verification for information other than the class itself. Whether the corresponding class can be found by the fully qualified name described by the string in symbol reference; whether the specified class contains a field descriptor that complies with the method and field described by the simple name ............
3. Preparation
The preparation phase is the phase in which the class variable is formally allocated memory and the initial value of the class variable is set. For non-static variables, no memory is allocated to them.
Initial Values of data types: int, byte, char, long, float, and double; default values: 0; boolean: false; reference: null.
For constant attributes (public staticFinalInt value = 23). The value is assigned directly to 23 in the preparation phase.
4. Analysis
In the parsing phase, the virtual machine replaces the symbol reference in the constant pool with the direct reference. A symbolic reference is a constant of CONSTANT_Class_info, CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info in the class format. A direct reference is a pointer directly pointing to a directory, relative offset, or an indirect first two to the destination. There are class or interface parsing, field parsing, class method parsing, interface method parsing.
5. Initialization
Is to initialize class variables and other resources, here the user's static field and static statement Block Value assignment operation will be executed. This process executes the process of the <clinit> () method of the class constructor.
<Clinit> () is generated when the mail compiler automatically collects the values of all class variables in the class and merges the statements in the static statement block (static, the sequence collected by the compiler is determined by the order in which the statements appear in the source file.
1) if the class has a parent class and the parent class is not initialized, initialize the direct parent class (execute <clinit> () of the parent class ()).
2) If the <clinit> () method exists before the makeup class, execute the <clinit> () method.
The interface cannot use static blocks.
Class Loader
Start the Bootstrap ClassLoader, which is mainly responsible for loading the directory java_home/lib or the path specified by the-Xbootclasspath parameter, it can also be a class library recognized by the VM to the memory of the VM.
Extension ClassLoader: This loader is created by sun. misc. launcher $ ExtClassLoader implementation, responsible for loading the java_home/lib/ext directory, or by java. ext. class Library in the path specified by dirs System Variables
Application ClassLoader: also called the system class loader. This class loader is created by sun. misc. launcher $ App-ClassLoader is mainly responsible for loading class libraries in the classPath path. If the application does not have a custom class loader, This is the default class loader.
These three types of loaders use a combination instead of a parent-child relationship. Under normal circumstances, when each class receives a class loading request, it will first call the parent loader for loading. If the parent loader fails to load, the Child loader will load. The sub-loader is called only when the parent loader cannot load the class.
If you need to load the java. lang. Object class, no matter which class loader is used for loading, you will find the root classLoader to load the class.