JVM Learning Note III: JVM class loading mechanism

Source: Internet
Author: User
Tags field table

Overview

The class file will eventually need to be loaded into the virtual machine before it can be run and used. 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.

Class loading process

Class starts from being loaded into the virtual machine's memory, and its entire lifecycle includes: loading, validating, preparing, parsing, initializing, using, and unloading 7 stages. Where validation, preparation, and parsing are collectively referred to as connections . The sequence of seven stages occurs as follows:

In Java, the loading, joining, and initialization of types are done during program runs. The order of the 5 phases of loading, validating, preparing, initializing, and unloading is deterministic, and class loading must begin in this order, and the parsing phase is not necessarily: in some cases, it can start after the initialization phase.

Load Loading time

There is no mandatory constraint in the Java Virtual Machine specification, which is grasped by the virtual machine implementation.

The process of loading
    1. Gets the binary byte stream that defines this class through the fully qualified name of the class;
    2. The static storage structure represented by the byte stream is transformed into the runtime data structure of the method area.
    3. Generate the Java.lang.Class object in the Java heap as the access entry for the data in the method area

It is important to note that for an array class, it is not created by the ClassLoader itself, but by the Java virtual machine directly. The load phase and part of the connection phase are cross-cutting.

Verify

Validation is the first step in the connection phase, which is intended to ensure that the information included in the byte stream of the class file conforms to the requirements of the current virtual machine.

Overall, the validation phase has roughly completed 4 phases of validation actions:
1. Validation of file formats
2. Meta-data validation
3. Byte code Verification
4. Symbol Reference Validation

File Format Verification

The first phase verifies that the byte stream conforms to the specification of the class file format, and the verification points are:

    • Whether to start with magic number 0xCAFFBABE
    • Whether the primary or minor version number is within the current Virtual machine processing range
    • Check the constant tag ID
      ......

The main purpose of this phase is to ensure that the input byte stream can be correctly parsed and stored in the method area . This phase is based on the binary byte stream, only through the verification of this phase, the byte stream will go into the memory of the method area for storage, so the next 3 verification phases are all based on the storage structure of the method area , no longer directly manipulate the byte stream.

Meta-data validation

The main purpose of the second stage is to make semantic validation of the metadata information of the class, and ensure that there is no metadata information that does not conform to the Java language specification. The verification points are as follows:

    • Does this class have a parent class
    • Whether the parent class of this class inherits a class that is not allowed to inherit
    • Does it implement all of the methods that are required to implement in its parent class or interface?
    • The field in the class, whether the method is inconsistent with the parent class (overriding the final field of the parent class, method overloads that do not conform to the rule)
BYTE code Verification

The main purpose is to determine that the program semantics are legal and logical through data flow and control flow analysis. After verifying the data types in the metadata information in the second phase, this phase verifies the method body of the class, ensuring that the method of the checked class does not make any event that endangers the security of the virtual machine at run time.

Symbol Reference Validation

The last phase of validation occurs when a virtual machine converts a symbolic reference to a direct reference, and the conversion action occurs during the third phase of the connection-the parsing phase.
The purpose of the symbol reference validation is to ensure that parsing is performed properly, and if it cannot be verified by a symbolic reference, a subclass of the Java.lang.IncompatibleClassChangError exception is thrown.

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 .

In this case, the memory allocation includes only the static modified variables, not the instance variables, followed by the initial value of the data type of the 0-value Public static int value = 123; preparation phase after the initial value of 0, not 123. Note: If it is a constant (final decoration), Public static final int value = 123; The initial value after the Prep phase is 123.

Analytical

The parsing phase is the process by which a virtual machine replaces the symbolic reference of a constant pool with a direct reference .

1, symbol Reference: A set of symbols to describe the referenced target, the symbol can be any form of literal, as long as the use 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 reference target is not necessarily loaded into memory
2, direct reference: A pointer directly to the target, 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 if there is a direct reference, the reference target must already be loaded into memory

The exact time that the resolution occurred

In the execution of Anewarray, Checkcast, GetField, Getstatic, instanceof, Invokeinterface, Invokespecial, invokestatic, Invokevitual, Multianewarray, New, Putfield, putstatic This 13 is used to manipulate the symbol reference before the bytecode directive.

Parsing process
    1. Parsing of classes or interfaces

      Assuming that the current code is in a constant pool with a class of d,d, a symbolic reference n is never parsed, and N is parsed as a direct reference to a class or interface C, the process is as follows:
      If C is not an array type: The fully qualified name of N is passed to the class loader of D to load C. If an exception occurs during the load process, the resolution fails
      If C is an array type, and the element type of the array type is an object, the descriptor of n is "[Ljava.lang.Integer": By the 1th load Integer, and then by the virtual machine generating an array object representing this array dimension and element;
      If the above step is not an exception, then C in the virtual machine has become a valid class or interface, but before parsing is complete, also need to do symbolic reference validation, confirm that D has access to C, or throw Java.lang.IllegalAccessError exception.

    2. Field resolution

      The Constant_class_info symbol reference for the Class_index item index in the field table is parsed first, that is, the symbolic reference of the class or interface to which the field belongs, denoted by C
      Then search for the fields in C:
      If C itself contains a field where both the simple name and the field descriptor match the target, the direct reference to the field is returned, and the lookup ends
      Otherwise, if C implements the interface, search for each interface and his parent interface from top to bottom according to the inheritance relationship. If the interface contains a field that has both a simple name and a field descriptor that match the target, the direct reference to the field is returned, and the lookup ends
      Otherwise, the parent class is searched from top to bottom by the inheritance relationship. If the parent class contains a field that has both a simple name and a field descriptor that matches the target, the direct reference to the field is returned, and the lookup ends
      Otherwise, the lookup fails, throwing a java.lang.NoSuchFieldError exception.
      If the lookup process returns to the app successfully, you also need permission validation on the field, which throws a Java.lang.IllegalAccessError exception if it does not pass.

    3. Interface Method Parsing
      Similar to field resolution

Initialization

The initialization phase is the last step in the class loading process, before you actually start executing the Java program code defined in the class.

Initialization time

There are only 4 cases in which the class must be initialized immediately:
1, encountered new (instantiated with the new keyword), getstatic (Gets a static field of a class, except for static fields decorated with the final modifier), putstatic (sets a static field of a class, Except for static fields that are decorated with the final modifier) and invokestatic (a static method that calls a class), this 4-bit bytecode directive must be initialized first if the class has not yet been initialized.
2, when you use a method in the Java.lang.reflect package to make a reflection call to a class, you must first initialize the class if it is not initialized.
3, when a class is initialized, if its parent class has not yet been initialized, it must first initialize its parent class
4, when the virtual machine starts, you need to specify a main class (the class where the Main method resides), the virtual opportunity preferred to initialize the main class
5, when using JDK1.7 's dynamic language support, if a java.lang.invoke.MethodHandle instance finally resolves the results ref_getstatic, ref_pullstatic, ref_ Invokestatic the method handle, and the class for the method handle has not been initialized, it is necessary to trigger its initialization first.

These 5 behaviors are called active references to a class, except that all references to the class do not trigger initialization, called a passive reference.

Initialization process

In the preparation phase, the variable has been assigned an initial value, in the initialization phase, according to the programmer's subjective plan to initialize the class variables and other resources, in short, the initialization phase is the virtual machine to execute the class constructor <clinit>() method of the process, the following details the following <clinit> method:

    • <clinit>Generated by the compiler's automatic collection of assignment actions for all class variables in a class and statements in a static statement block, the order in which the compiler collects is determined by the order in which the statements appear in the source file, with special care that the static statement block can only access the class variables that were defined before it. A class variable defined after it can only be assigned a value and cannot be accessed
publicclass Test{    static{        0;               //给变量赋值可以正常编译通过        System.out.print(i);  //这句编译器会提示‘非法向前引用’     }}
    • Unlike instance constructor <init>() methods, <clinit> methods do not require explicit invocation of the parent class's method, and the virtual machine automatically guarantees that the method of the <clinit>() parent class is completed before the method of the subclass is <clinit>() executed, and the <clinit>() first execution <clinit>() of the VM The class of the method isjava.lang.Object
public  class  superclass  { static  {System.out.println ( "super class init!" ); }  }  
publicclass SubClass extends SuperClass{      static{          System.out.println("sub class init!");      }      publicstaticvoidmain(String[] args) {          new SubClass();      }  }  

The operating result is:

Super Class init!
Sub Class init!

    The
    • <clinit> () method is not required for a class or interface , and if a class does not contain static statement blocks and does not have an assignment to a class variable, the compiler can not generate for that class Static statement blocks are not allowed in the <clinit> () method interface, but can have assignment of class variables, so the compiler also generates the <clinit> () method on the interface, executing the interface's The <clinit> () method does not require that the <clinit> () method of the parent interface be executed first unless the class variable initialized in the parent interface is used in the interface, The interface implementation class does not necessarily require that the interface's <clinit> () method be executed first

    • Virtual opportunity to guarantee the <clinit> () of a class Methods are correctly locking and synchronized in a multithreaded environment , if multiple threads initialize a class at the same time, only one thread executes the <clinit> () method of the class, and the other threads block the wait. Until the active thread executes the <clinit> () method is complete

publicclass A {      static{          System.out.println(Thread.currentThread().getName()                  +" "+new Date());          try {              Thread.sleep(5000);          catch (InterruptedException e) {              e.printStackTrace();          }      }  }  
 Public  class T implements Runnable {       Public void Run() {A A =NewA (); System.out.println (Thread.CurrentThread (). GetName () +" "+NewDate ()); } Public Static void Main(string[] args) {T T1 =NewT (); T t2 =NewT (); Thread Thread1 =NewThread (t1); Thread thread2 =NewThread (T2);          Thread1.start ();      Thread2.start (); }  }

The results of the operation are as follows:

Thread-0 Fri Dec 10:25:00 CST 2013
Thread-1 Fri Dec 10:25:05 CST 2013
Thread-0 Fri Dec 10:25:05 CST 2013

JVM Learning Note III: JVM 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.