JVM-low-level loading

Klass model

  • Inheritance structure of klass model class
    Insert picture description here
    As can be seen from the above figure: the meta information of the class is stored in the meta space, which is the method area
    In the JVM, the common class corresponds toinstanceKlassInstance of the class.
  • InstanceMirrorKlass: Used to indicate that the Class object obtained in java.lang.Class, java code is actually an instance of this C++ class. The Class object that is stored in the heap area, that is, we reflect, is an OOPKlass.
  • InstanceRefKlass: used to represent a subclass of java/lang/ref/Reference
  • InstanceClassLoaderKlass: Used to traverse a class loaded by a loader.
    The array in Java is not a static data type, but a dynamic data type, which is generated during runtime. The meta information of the Java array is usedArrayKlassTo represent
  • TypeArrayKlass: used to represent the array of basic types
  • ObjArrayKlass: used to represent an array of reference types

Class loading process

The life cycle of a class is composed of 7 stages, but the loading of the class refers to the first 5
Insert picture description here
stages. The order of the 5 stages of loading, verification, preparation, initialization and unloading is determined. The type of loading process must be installed and this sequence starts step by step. That is to say, the beginning is in this order, but it is not possible to proceed after the first is completed.

load

  • Obtain the binary byte stream storing the class file of the class through the fully qualified name of the class (there is no requirement to obtain it there, so it can be obtained through extension, network, zip, etc.)

  • Convert the static storage structure represented by this byte stream lock into the runtime data structure of the method area

  • Generate a java.lang.Class object representing this class in memory, which is InstanceKlass.Put into the method area as a runtime data structure.
    The above 3 points are the key to connecting the file and the Openjdk source code. So the written program needs to complete the above 3 effects.

verification

  • File format verification Verify
    that the byte stream conforms to the Class file format specification and can be processed by the current version of the virtual machine.
  • Metadata verification
    Semantic analysis of the information described by the bytecode. Whether there is a parent class, whether the parent class is reasonable, etc. . .
  • Bytecode verification
    Through data flow analysis and control flow analysis, determine whether the semantics are legal and reasonable. After verifying the data type in the metadata information in the second stage, the Code should be analyzed.
  • Symbol reference verification
    This phase occurs when the virtual machine converts the symbol reference to a direct reference (this happens during the resolution phase).

ready

The preparation stage is the stage to formally allocate memory for the variables (static variables) defined in the class and set the initial values ​​of the variables. The so-called initialization value is a zero value. Only the value modified by final is allocated at compile time, and initialization will be displayed in the preparation phase.

Parsing

Convert symbolic references to direct references.

  • Symbol reference
    Symbol reference uses a set of symbols to describe the referenced target, and you can locate the target when you use it.
  • Direct references
    Direct references are pointers, relative offsets, or handles that can directly point to the target.
    The analysis action is mainly for: class or interface, field, class method, interface method, method type, etc.

initialization

The initialization phase initializes class variables and other resources according to the plan area specified by the programmer through the code. Run the clinit() method. Instance variables will allocate instance variable space in the heap space and perform default assignments when the object is created.

  • clinit() generation process
    • clinit() is the assignment action and static statement block of all variables in the class automatically collected by the compiler. The order of collection is determined by the order in which the sentences appear in the source file.
    • The clinit() method is not the same as the class constructor. It does not need to explicitly call the parent class constructor, because jvm will ensure that the parent class has been executed before the subclass clinit() is executed.
    • The execution method will lock synchronously
      Initialization start condition
    • 遇到new, getstatic,putstatic,invokestatic
      • Encountered the New keyword to instantiate the object
      • Read to get set a type of static field
      • Call a static method
    • Use reflect reflection
    • When initializing the class, if the parent class is not initialized, you need to initialize the parent class first
    • When the virtual machine starts, load and initialize the main class
    • When using JDK7 to join, when the new dynamic language support is added.
    • Use JDK8 to newly add the default method. If there is an interface that implements this class to initialize, then this interface is initialized before it.

Guess you like

Origin blog.csdn.net/null_zhouximin/article/details/112493127