<< deep Java Virtual Machine >>-virtual machine class loading mechanism-learning notes

Source: Internet
Author: User

Time of class loading
    • When you encounter the 4 bytecode directives of new, getstatic, putstatic, or invokestatic, if the class has not been initialized, you need to trigger its initialization first. The most common Java scenario for generating these 4 instructions is when instantiating an object using the New keyword, reading or setting a static field of a class (except for a static variable that is final decorated, has placed the result into a constant pool at compile time), and when invoking a static method of a class.
    • When you use the Java.lang.reflect package method to make a reflection call to a class, it is necessary to trigger its initialization if the class is not initialized.
    • When initializing a class, it is found that its parent class has not yet been initialized, it is necessary to first trigger the initialization of the parent class.
    • When the virtual machine starts, the user needs to develop a main class to execute (the class that contains the main () method), and the virtual opportunity initializes the class first.
Class loading process

Class from being loaded into the virtual machine memory, to unloading out of memory, its entire lifecycle is divided into: Load (Loading), validate (verification), prepare (preparation), Parse (Resolution), Initialization (initialization), use (using), and unload (unloading) seven stages. Where the validation, preparation, and parsing of three parts is collectively referred to as the connection (linking).

Load

  • During the load phase, the virtual machine needs to complete the following three things:

    • A binary byte stream that defines this class is obtained through the fully qualified name of a class.
    • Converts the static storage structure represented by this binary byte stream into the runtime data structure of the method area.
    • Generate a Java.lang.Class object in the Java heap that represents this class as the access entry for the data in the method area.
  • Validation

    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.

    Different virtual machines implement different implementations of class validation, but generally complete the following four phases of the verification process:

    • file format validation

      This stage verifies that the byte stream conforms to the class file format specification and can be processed by the current version of the virtual machine. For example, whether to start with the magic number 0xCAFEBABE (which is a class file that can be used by a virtual machine), whether the main or minor version number is in the current virtual machine's processing range, or whether there are constants in various index values that point to constants that do not exist or that do not conform to a type The individual parts of the class file and the file itself have additional information that is deleted or attached.

      The primary purpose of this validation phase is to ensure that the input byte stream is correctly parsed and stored in the method area, in a format that conforms to the requirements for describing a Java type of information.

      This phase of validation is based on the byte stream, after this phase of validation, the byte stream will go into the memory of the method area for storage, so the subsequent three verification phase is based on the storage structure of the method area.

    • Metadata Validation

      The second phase is semantic analysis of the information described in bytecode to ensure that the information described conforms to the requirements of the Java language specification.

      such as whether the class has a parent class (except for Java.lang.Object, the rest of the classes have a parent class), whether the parent class inherits the class that is not allowed to inherit (the final decorated class), and if the class is not an abstract class, implements all the methods required in its parent class or interface. 。

    • Bytecode validation

      The third phase is one of the most complex phases of the entire validation process, with the main task being to perform data flow and control flow analysis. This phase verifies the method body of the class.

      For example, it is safe to ensure that type conversions in the method body are valid, such as the ability to assign a subclass object to the parent data type, but it is dangerous and illegal to assign the parent object to the subclass data type.

    • Symbol Reference validation

      The last phase of a validation occurs when a virtual machine converts a symbolic reference to a direct reference.

      For example, if the fully qualified name in the symbol reference is described by a string, the corresponding class is found, and the access to the class, field, and method referenced by the symbol (private, protected, default, public) can be accessed by the current class.

  • The
  • prepare

    Preparation phase is a phase that formally allocates memory for class variables and sets the initial value of class variables, which are allocated in the method area. This time the memory allocation includes only class variables (variables that are modified by static), not instance variables, and instance variables are allocated in the Java heap along with the objects as they are instantiated. Next, the initial value, "Typically," is a 0 value of the data type, assuming a class variable is defined as:

     public  static  int  value = 123; 

      The initial value of the variable value after the prep phase is 0 instead of 123 because no Java method has been executed at this time, and the putstatic instruction that assigns value 123 is that the program is compiled and stored in the class constructor <clinit > () method, the action to assign value to 123 will not be executed until the initialization stage.

    For "special cases" means:

     public  static  final  int  value = 123; 

      If the Constantvalue property exists in the field property sheet of a class field, it is initialized to the specified value in the prepare initial phase, so after the label is final, value is initialized to 123 instead of 0 in the prepare phase.

  • The
  • parse

    Resolution stage is the process by which a virtual machine replaces a symbolic reference in a constant pool with a direct reference.

    • symbol reference (symbolic References): a symbol reference is a set of symbols that describe the referenced target, and the symbol can be any form of literal, as long as it can be used without ambiguity to locate the target. The symbol refers to the memory layout independent of the virtual machine implementation, and the referenced target does not necessarily load into memory.
    • Direct 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 symbol reference is generally different from the direct references that are translated on different virtual machine instances. If there is a direct reference, then the target of the reference must exist in memory. The

    Parsing action is primarily for classes or interfaces, fields, class methods, interface methods, and four types of symbolic references.

  • Initialization

    The class initialization phase is the last step in the class loading process, in which the Java program code defined in the class is actually executed.

    In the preparation phase, constants have already been assigned the initial value of the system requirements, while in the initialization phase, the variables of the class are initialized according to the programmer's subjective initialization process. Or from another point of view: 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{} block), the order in which the compiler collects 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, a variable that is defined after it, and is assignable in the preceding static statement block, but cannot be accessed.
    • The <clinit> () method differs from the constructor of a class in that it does not require a display to call the parent class constructor, and the virtual opportunity guarantees that the <clinit> () method of the parent class has been executed before the subclass's <clinit> () method executes.
    • If there is a variable assignment operation in the interface, the interface will generate the <clinit> () method as well as the class. However, unlike classes, the <clinit> () method of the execution interface does not need to perform the parent interface's <clinit> () method first, and the parent interface is initialized only if the variables in the parent interface are used. In addition, the implementation class of the interface does not execute the <clinit> () method of the interface at initialization time.
    • The virtual opportunity guarantees that a class <clinit> () method is properly locked and synchronized in a multithreaded environment, and if multiple threads initialize a class at the same time, only one thread executes the <clinit> () method of the class, and other threads need to block the wait.

<< deep Java Virtual Machine >>-virtual machine class loading mechanism-learning notes

Related Article

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.