Class loading process
Class starts from being loaded into the virtual machine's memory, and its entire lifecycle includes: Load (Loading), validate (verification), prepare (preparation), Parse (Resolution), Initialize ( initialization), use (using), and unload (unloading) 7 stages. Where preparation, validation, parsing 3 parts collectively referred to as the connection (linking):
The order of the 5 stages of loading, validating, preparing, initializing, and unloading is determined, and the load process of the class must begin in this order, and the parsing phase is not necessarily: it can be started after the initialization phase in some cases, This is to support runtime bindings for the Java language (also known as dynamic binding or late binding). The following statements are in the context of hotspot.
Load
During the load phase (you can refer to Java.lang.ClassLoader's LoadClass () method), the virtual machine needs to complete the following 3 things:
- A binary byte stream that defines this class is obtained through the fully qualified name of a class (it is not specified to be obtained from a class file, from other channels, such as: network, dynamic generation, database, etc.);
- Transform the static storage structure represented by this byte stream into the runtime data structure of the method area;
- Generate a Java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area;
Portions of the load phase and connection phase (linking), such as some bytecode file format validation actions, are interleaved, the loading phase is not complete, the connection phase may have begun, but the actions taken during the loading phase of these clips are still part of the connection phase, The start time of the two phases remains in a fixed sequence.
Verify
Validation is the first step in the connection phase, which is designed to ensure that the information contained in the byte stream of a class file conforms to the requirements of the current virtual machine and does not compromise the security of the virtual machine itself.
The validation phase will roughly complete the 4-phase inspection action:
- File Format verification: Verify that the byte stream conforms to the specification of the class file format, for example: whether to start with magic 0xCAFEBABE, whether the primary and secondary version number is within the processing range of the current virtual machine, and whether the constants in the constant pool have unsupported types.
- Meta-data validation: Semantic analysis of the information described in bytecode (note: Comparing the semantic analysis of the JAVAC compilation phase) to ensure that the information it describes conforms to the requirements of the Java language specification, for example: Whether this class has a parent class, except Java.lang.Object.
- Bytecode verification: Through data flow and control flow analysis, it is reasonable to determine that the program semantics are legal and logical.
- Symbol Reference Validation: Ensures that parsing actions are performed correctly.
The validation phase is important, but not necessary, and it has no effect on the program runtime, and if the referenced classes are repeatedly validated, consider using the-xverifynone parameter to turn off most of the class validation measures to shorten the load time of the virtual machine class.
Get ready
The prep phase is a phase that formally allocates memory for class variables and sets the initial value of class variables, and the memory used by these variables is allocated in the method area. The memory allocation at this time includes only class variables (variables that are modified by static), not instance variables, and instance variables are allocated to the heap as objects are instantiated. Second, the initial value referred to here is the 0 value of the data type, assuming that a class variable is defined as:
1 |
publicstaticintvalue=123; |
The initial value of the variable value after the prep phase is 0 instead of 123. Since no Java methods have been executed at this time, the putstatic instruction that assigns value 123 is that the program is compiled and stored in the class constructor () method. So an action that assigns value to 123 will not be executed until the initialization stage.
As for "special case" means: public static final int value=123, that is, when the field property of a class field is Constantvalue, it is initialized to the specified value in the preparation phase, so after the label is final, Value is initialized to 123 instead of 0 in the prepare phase.
Analytical
The parsing phase is the process by which a virtual machine replaces a symbolic reference within a constant pool with a direct reference. Parsing actions are primarily for classes or interfaces, fields, class methods, interface methods, method types, method handles, and call Point qualifier 7 class symbol references.
Initialization
The class initialization phase is the last step in the class loading process, and in the initialization phase, the Java program code defined in the class is really started. In preparation for extremes, the variable has already paid the initial value of the system requirement, and during the initialization phase, the class variables and other resources are initialized according to the program's supervisor plan, or the initialization phase is the process of executing the class constructor <clinit> () method.
The <clinit> () method is generated by the compiler's automatic collection of assignment actions for all class variables in the class and statements in the static statement block static{}, and the order in which the compilers are collected is determined by the order in which the statements appear in the source file. A static statement block can only access variables that are defined before a static statement block, define variables after it, and in the preceding static statement block may be assigned values, but cannot access
Java class loading process