Home >Java >javaTutorial >Java Virtual Machine Learning - Class Loading Mechanism
The loading mechanism is the process in which the JVM loads the class file into the memory, verifies, converts, parses and initializes the data, and finally forms a Java type that can be directly used by the JVM.
From the time a class is loaded into the virtual machine memory to the time it is unloaded from the memory, its life cycle includes: Loading, Verification, Preparation, Resolution, There are seven stages: Initialization, Using, and Unloading, among which the three parts of verification, preparation, and parsing are collectively referred to as links.
The order of the five stages of loading (loading), verification, preparation, initialization and unloading is fixed. The loading process of the class must start in this order, and the parsing stage Not necessarily; it can in some cases start after initialization, for the runtime dynamic binding feature. It is worth noting that these stages are usually intertwined and mixed, usually calling or activating another stage during the execution of one stage.
The loading stage is a stage in the "class loading mechanism". This stage is usually also called "loading" and mainly completes:
1. Get the binary byte stream that defines this class through the "full name of the class"
2. Convert the static storage structure represented by the byte stream into runtime data in the method area Structure
3. Generate a java.lang.Class object representing this class in the java heap as the access entry for these data in the method area
Virtual machine specifications for "through" the full name of the class "To obtain the binary byte stream that defines this class" does not specify that the binary stream must be obtained from a local class file. To be precise, it does not specify where and how to obtain it. For example:
Reading from Zip packages is very common and eventually becomes the basis for future JAR, EAR, and WAR formats.
Obtained from the Internet, common application Applet.
Run-time calculation and generation. The most commonly used in this scenario is dynamic proxy technology. In java.lang.reflect.Proxy, ProxyGenerator.generateProxyClass is used to generate the binary of $Prxoy's proxy class for a specific interface. byte stream.
is generated from other format files. Typical scenario: JSP application
reads from the database. This scenario is relatively rare. Some middleware servers (such as SAP Netweaver) can choose to install the program to Database to complete the distribution of program code among clusters.
Compared with other stages of the class loading process, the loading stage (preparatory speaking, the action of obtaining the binary byte stream of the class in the loading stage) is the most controllable stage during the development period, because the loading stage This can be done using the class loader (ClassLoader) provided by the system, or it can be done by a user-defined class loader. Developers can control the acquisition method of the byte stream by defining their own class loader.
After the loading phase is completed, the binary byte stream outside the virtual machine is stored in the method area according to the format required by the virtual machine. The data storage format in the method area is defined by the virtual machine implementation. The specific data structure of this area is not specified. Then an object of the java.lang.Class class is instantiated in the java heap. This object serves as the external interface for the program to access these types of data in the method area. Parts of the loading phase and the linking phase (such as part of the bytecode file format verification actions) are interleaved. The loading phase has not yet been completed, and the linking phase may have started, but these actions sandwiched between the loading phase still belong to the link. The contents of the stages and the start times of these two stages still maintain a fixed sequence.
Carry out data flow and control flow analysis. At this stage, the method body of the class is verified and analyzed. The task of this stage is to ensure that the method of the class being verified will not perform behavior that endangers the security of the virtual machine during runtime. For example: ensure that the type conversion in the access method body is valid. For example, you can assign a subclass object to a parent class data type. This is safe, but you cannot assign a parent class object to a subclass data type. Ensure that the jump command does not Will jump to bytecode commands outside the method body.
4. Symbol reference verification
Whether the fully qualified name described by the string in the symbol reference can find the corresponding class, the accessibility of the classes, fields and methods in the symbol reference class (private , protected, public, default) can be accessed by the current class.
The preparation stage is the stage where memory is officially allocated for class variables and the initial values of class variables are set. These memories will be performed in the method area. distribute. There are two knowledge points that are easily confusing at this stage. First, memory allocation at this time only includes class variables (static modified variables), not instance variables. Instance variables will follow the object when the object is instantiated. allocated together in the java heap. Secondly, the initial value mentioned here is "normally" the zero value of the data type. Suppose a class variable is defined as:
public static int value = 12;
Then the variable value is The initial value after the preparation phase is 0 instead of 12, because no Java method has been executed yet, and the putstatic instruction that assigns value to 123 is stored in the class constructor
The initial value is zero under the "normal circumstances" mentioned above. Compared with some special cases, if there is a ConstantValue attribute in the field attribute table of the class field, then the variable value will be changed during the preparation phase. Initialized to the value specified by the ConstantValue attribute, the above class variable value is defined as:
public static final int value = 123;
When compiling, javac will generate the ConstantValue attribute for value. In preparation The stage virtual machine will set the value to 123 according to the ConstantValue setting.
The parsing phase is the process of replacing symbol references in the virtual machine constant pool with direct references.
Symbol reference: A symbol reference is a set of symbols to describe the referenced target object. The symbol can be any form of literal, as long as the target can be located unambiguously when used. Symbolic references have nothing to do with the memory layout implemented by the virtual machine, and the referenced target object does not necessarily have to be loaded into memory.
Direct reference: A direct reference can be a pointer directly pointing to the target object, a relative offset, or a handle that can indirectly locate the target. Direct references are related to the implementation of virtual machine memory layout. The direct references translated from the same symbol reference on different virtual machine instances are generally not the same. If there is a direct reference, the reference target must already exist in the memory.
The virtual machine specification does not stipulate the specific time when the parsing phase occurs. It only requires the execution of 13 steps: anewarry, checkcast, getfield, instanceof, invokeinterface, invokespecial, invokestatic, invokevirtual, multianewarray, new, putfield and putstatic. Before the bytecode instructions used to operate symbol references, the symbol references they use are parsed first, so the virtual machine implementation will judge based on needs whether the symbol references in the constant pool are processed when the class is loaded by the loader. Parse, or wait until a symbol reference is about to be used before parsing it.
The parsing action is mainly performed on four types of symbol references: classes or interfaces, fields, class methods, and interface methods. Corresponds to the four constant types CONSTANT_Class_Info, CONSTANT_Fieldref_Info, CONSTANT_Methodef_Info, and CONSTANT_InterfaceMethoder_Info in the compiled constant pool respectively.
1. Class and interface analysis
2. Field analysis
3. Class method analysis
4.Interface method analysis
The initialization phase of a class is the last step of the class loading process. In the preparation phase, the class variable has been assigned the initial value required by the system, and in the initialization phase , it is to initialize class variables and other resources according to the subjective plan made by the programmer through the program, or it can be expressed from another perspective: the initialization phase is the process of executing the class constructor
1. When encountering the four bytecode instructions of new, getstatic, putstatic or invokestatic, if the class has not been initialized, it needs to be triggered first its initialization. The most common Java code scenarios that generate these four instructions are: using the new keyword to instantiate an object, reading or setting static fields of a class (except for static fields that are modified by final and have been put into the constant pool by the compiler) ), and when calling static methods of the class.
2. When using the method of the java.lang.reflect package to make a reflection call to a class
3. When initializing a class, if it is found that its parent class has not been initialized, You need to start the initialization of its parent class first
4. When jvm starts, the user specifies a main class for execution (the class containing the main method), and the virtual machine initializes this class first.
In the above preparation stage, public static int value = 12; In preparation After the stage is completed, the value of value is 0, and the class constructor
*The class constructor
*The class constructor
*Since the
*
*Static statement blocks cannot be used in interfaces, but what is not possible with interfaces and classes is that executing the
*The virtual machine ensures that the
The above is the content of Java Virtual Machine Learning - Class Loading Mechanism. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!
Related articles:
Detailed explanation of Java virtual machine
In-depth understanding of Java virtual machine
Java Virtual Machine Learning - Object Memory Allocation and Recycling