Java Virtual machine Note (a): Class loading mechanism

Source: Internet
Author: User

I. Overview

The virtual machine loads the data of the description class from the class file into the memory, verifies the data, parses and initializes it, and finally forms the Java type that can be used directly by the virtual machine, which is the class loading mechanism of the virtual machine.

Ii. life cycle of class loading

Class starts from being loaded into the virtual machine's memory, and its entire lifecycle includes: Load (Loading), validate (verification), prepare (preparation), Parse (Resolution), Initialization (initialization), use (using), and unload (unloading) 7 stages.

Where: validation, preparation, parsing 3 parts collectively referred to as the connection (linking).

The order of the 5 phases of loading, validating, preparing, initializing, and unloading is determined, and the class's loading process must begin in this order, and the parsing phase does not necessarily:

In some cases, you can start after the initialization phase, which is to support runtime bindings for the Java language (also known as dynamic binding or late binding), where the "step-by-start" is not a chain, and these phases are often mixed in a cross-section, usually called during a phase of execution, Activates another stage.

Third, the class loading process 1. Load

Load is a phase of the class loading process. During the load phase, the virtual machine needs to complete the following 3 things:

1) a binary byte stream that defines this class is obtained through the fully 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 representing this class in memory as a variety of data access portals for the class of the method area.

After the loading phase is complete, the binary byte stream outside the virtual machine is stored in the method area in the format required by the virtual machine, and the data storage format in the method area is customized by the virtual machine, and the virtual machine specification is the specific data structure that defines the area. Then instantiate an object of the Java.lang.Class class in memory (not explicitly defined in the Java heap, for a hotspot virtual machine, the class object is special, although it is an object, but stored in the method area), this object will be used as a program to access these types of data in the method area of the external interface.

The loading phase and part of the connection phase are interleaved, the loading phase is not complete, the connection phase may have begun, and when these clips are performed during the loading phase, they are still part of the connection phase, and the start time of the two phases remains in a fixed sequence.

2. Verification

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.

4 Test actions:

1) file Format verification

    • Whether to start with the magic number Oxcafebabe
    • Whether the primary or minor version number is within the current virtual machine processing range.
    • Constants in the constant pool are not supported by the constant type (check the constant tag flag).
    • The various index values that point to a constant have a constant that does not exist or that does not conform to a type.
    • Whether there is data that does not conform to UTF8 encoding in the constants of the Constant_utf8_info type.
    • The individual sections of the class file and the file itself have additional information about the deleted live append.
    • ......

2) meta-data validation

    • Whether this class has a parent class (except for Java.lang.Object, all classes should have a parent class).
    • Whether the parent class of this class inherits a class that is not allowed to be inherited (a class that is final decorated).
    • If this class is not an abstract class, does it implement all of the methods that are required to be implemented in its parent class or interface.
    • The fields in the class, whether the method is inconsistent with the parent class (for example, the final field that overrides the parent class, the method overloads that do not conform to the rule, such as method parameters are consistent when the return value type goes different, etc.).
    • ......

3) Byte code verification

    • Ensure that the data type of the operand stack at any time and the sequence of instruction codes work together, for example, there is no such thing as a case where an int type of data is placed on the action stack, but is loaded into a local variable by a long type when used.
    • Ensure that the jump instruction does not jump on bytecode instructions other than the method body.
    • Guaranteed type conversion in the method body is valid, such as the ability to assign a subclass object to the parent data type, but assigning the parent object to the subclass data type, or even assigning the object to a data type that has no inheritance and is completely irrelevant to it, is dangerous and illegal. 、
    • ......

4) symbol Reference validation

    • The fully qualified name that is described by a string in the symbol reference can find the corresponding class.
    • In the development class, there is a field descriptor that conforms to the method and the method and field described by the simple name.
    • The class, field, and method's accessibility (private, protected, public, default) in the symbol reference can be accessed by the current class.
    • ......

3. Preparation

The prep phase is the stage in which memory is allocated for class variables and the initial values of class variables are set, and the memory used by these variables is allocated in the method area.

Note: The memory allocation at this time includes only class variables (static variables, which are statically modified variables), not actual variables, and the actual variables will be allocated in the Java heap along with the object when the object is instantiated.

4. Parsing

The virtual machine replaces the symbolic reference in the constant pool with the process of direct referencing, conforming to the reference in the Class file to Constant_class_info, Constant_fieldref_info, constant_methodref_ Constants for types such as info are present.

♦ symbol Reference (symbolic References)

A symbol reference describes the target in a set of symbols, which can be any form of literal, as long as it can be used without ambiguity to locate the target. The symbolic reference is independent of the memory layout implemented by the virtual machine, and the referenced target is not necessarily loaded into memory. The memory layouts implemented by various virtual machines can be different, but the symbolic references they can receive must be consistent, because the literal form of the symbolic reference is clearly defined in the Java Virtual Machine specification's class file format.

♦ Direct Reference (directly References)

A direct reference can be a pointer to a target directly, a relative offset, or a handle that can be indirectly anchored to the target. The direct reference is related to the memory layout implemented by the virtual machine, and the same symbolic reference is generally not the same as the direct reference translated on different virtual machine instances. If there is a direct reference, the referenced target must already exist in memory.

5. Initialization

Executes the Java program code (or bytecode) defined in the class, initializing variables and other resources. It can also be said to be the process of executing the class constructor <clinit> () method.

Main content: "In- depth understanding of Java Virtual machines: JVM advanced features and best practices "

Java Virtual machine Note (a): Class loading mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.