Home > Java > Java Tutorial > body text

Java Virtual Machine Learning - Class Loading Mechanism

黄舟
Release: 2017-03-18 17:51:55
Original
1163 people have browsed it

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.


Loading:

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.


#Verification:

Verification is the first step in the link phase. The main purpose of this step is to ensure that the information contained in the byte stream of the class file meets the requirements of the current virtual machine and will not endanger the security of the virtual machine itself. .

The verification phase mainly includes four verification processes: file format verification, metadata verification, bytecode verification and symbol reference verification.

1. File format verification

Verify the class file format specification, for example: whether the class file starts with magic 0xCAFEBABE, whether the major and minor version numbers are within the processing range of the current virtual machine, etc.

2. Metadata verification

This stage is to conduct semantic analysis of the information described by the bytecode to ensure that the described information meets the requirements of the Java language specification. Verification points may include: whether this class has a parent class (except java.lang.Object, all classes should have a parent class), whether this class inherits a class that is not allowed to be inherited (modified by final), and if The parent class of this class is an abstract class. Does it implement all the methods required to be implemented in the parent class or interface?

3.Bytecode verification

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.


Preparation:

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 () method after the program is compiled. , so the action of assigning value to 12 will only be executed during the initialization phase.

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.


Parsing:

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


Initialization:

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() method. The initialization process will be triggered in the following four situations:

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 () method is called in the initialization stage. After the stage is completed, the value of value is 12.

*The class constructor() method is generated by the compiler automatically collecting the assignment actions of all class variables in the class and merging the statements in the static statement block (static block). The compiler collects The order is determined by the order in which the statements appear in the source file. In a static statement block, only variables defined before the static statement block can be accessed. Variables defined after it can be assigned values ​​in the previous static statements, but Can not access.

*The class constructor () method is different from the class constructor (instance constructor () method). It does not need to explicitly call the parent class constructor. The virtual machine is guaranteed to be Before the () method of the subclass is executed, the () method of the parent class has been executed. Therefore, the class of the first () method executed in the virtual machine must be java.lang.Object.

*Since the () method of the parent class is executed first, it means that the static statements defined in the parent class will take precedence over the variable assignment operations of the subclass.

*() method is not necessary for classes or interfaces. If there are no static statements in a class and no variable assignment operations, the compiler does not need to generate< for this class. ;clinit>() method.

*Static statement blocks cannot be used in interfaces, but what is not possible with interfaces and classes is that executing the () method of the interface does not require first executing the () method of the parent interface. The parent interface will be initialized only when the variables defined in the parent interface are used. In addition, the implementation class of the interface will not execute the () method of the interface during initialization.

*The virtual machine ensures that the () method of a class is correctly locked and synchronized in a multi-threaded environment. If multiple threads initialize a class at the same time, only one thread will execute this For the () method of the class, other threads need to block and wait until the active thread completes executing the () method. If there are long-running operations in the () method of a class, it may cause multiple processes to be blocked.

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 (www.php.cn)!

Related articles:

Detailed explanation of Java virtual machine

In-depth understanding of Java virtual machine

Java Virtual Machine Learning - Object Memory Allocation and Recycling

Java Virtual Machine Learning - Object Access

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!