What is the class loading mechanism
The JVM loads class description data file into memory from Class, and verify the data, and converts parsing initialization, forming Java type can be directly used in the JVM, which is the JVM class loading mechanism.
If you are not familiar with the structure of Class file, you can refer to the article before the comprehensive analysis of Class file structure (on) and Class comprehensive analysis of the file structure (lower) .
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
Class life cycle
From the class is loaded into memory, the memory is unloaded to the total divided into the following steps:
- Load (Loading)
- Verification (Verification)
- Preparation (Preparation)
- Parsing (Resolution)
- Initialization (Initialization)
- Use (Using)
- Uninstall (Unloading)
Class loading the whole process, which comprises the loading , verification , preparation , parsing , initialization phases.
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
load
Load class is the first stage of loading, in this step JVM specification required to complete the following three things:
- To obtain a binary byte stream class definition by the fully qualified name of a class.
- Static storage structure represented by the byte stream into multiple run-time data structure area method.
- Generating a representative of this class java.lang.Class objects in memory.
In fact, the above requirements are not specific, JVM specific implementations and applications are more flexible. For example: Get this class binary byte stream, and where to obtain not say, how to obtain, so there is read (jar, war, ear) from the archive, the acquisition (the Applet) from the network, the compute runtime generating (dynamic proxy). For the load is not an array of class, we can define your own class loader to control the manner of obtaining the byte stream. However, for an array class is not the same as the class itself is not an array created by the class loader, but the JVM created directly.
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
verification
This phase is to ensure that the information byte stream Class file contained in line with current requirements of the JVM, JVM and do not endanger its own security. Roughly divided into the following four stages:
File format validation
Verify the byte stream for compliance with Class file format, this can not be processed by the JVM. More verification points, such as: whether the beginning of the magic number 0xCAFEBABE, major and minor version numbers are within the scope of the current process JVM, constants constant pool of whether there is a constant unsupported type, CONSTANT_Utf8_info type of constant whether non-compliance with UTF8 the encoded data and the like. This stage is the verification flow based on binary bytes, only at this stage validated by the byte stream to enter the memory storage area method.
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
Metadata validation
This stage is mainly metadata information such semantic analysis and verification, to ensure that metadata information is not compliant with the Java language specification does not exist. For example: if there is in addition to the class other than the parent class java.lang.Object, whether inherited an inherited class is not permitted, whether the non-abstract class implements all the methods of its parent class or interface implementation requirements, whether covered parent the final field, and so on.
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
Bytecode verifier
By this stage the data flow and control flow analysis to ensure that the program is legal semantics, logical. For example: When placing the stack data types and operations used to ensure consistent, will not guarantee the jump instruction to jump to the bytecode instructions other than the method body, to ensure that body type conversion method is effective and the like.
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
Symbolic reference check
This phase is the information other than the type itself (various symbols constant pool reference) matching check, it occurs in the analytic step, to ensure analytical normal execution, such as: a reference symbol defined by a string of full-described if you can find the name of the corresponding class, whether the access method of class field of symbolic reference can access the current class, and so on.
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
ready
At this stage, to allocate memory for static variables and set a static variable initial values. The initial value here that is usually the case, the initial value of the code is not written, but the zero value of the data type. Code written initial value in the initialization assignment phase. If it is a static constant (the final modification), this stage will be directly assigned to write code initial value.
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
Resolve
At this stage, JVM symbolic references to the constant pool is replaced with a direct reference. Symbol reference to a certain set of symbols described in the referenced, literal symbols may be in any form, can be unambiguously positioned to the target can be used as long as, it is irrelevant, and the memory layout JVM implementation. A direct reference may be a direct pointer to the target, a relative offset or indirectly targeted to handle the target, and it is the memory layout JVM implementation dependent. If you have a direct reference, the target reference must exist in memory.
Analytical primarily for the class or interface, fields, methods class, interface method, type method, and the method handle calls for symbolic reference points qualifiers, respectively corresponding to the constant pool CONSTANT_Class_info, CONSTANT_Fieldref_info, CONSTANT_Methodref_info, CONSTANT_InterfaceMethodref_info, CONSTANT_MethodType_info, CONSTANT_MethodHandle_info and CONSTANT_InvokeDynamic_info.
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
initialization
Really started the initialization phase defined in the class bytecode, but also perform the class constructor
JVM will ensure that the subclass
JVM will ensure a class
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.
Epilogue
This time we know the class loading process of several stages, namely, loading , validation , preparation , analytical and initialization . Loading is the binary byte code into memory, authentication information included in the check byte stream meets the requirements when, to prepare a memory allocated as a static variable and a static variable initial value setting, parsing is to replace symbolic reference constant pool is a direct quote, initialization is executed all motion and static variables static assignment statement statement in the block.
Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.