JVM bottom-class loading and oop-klass model
The following is for jdk1.8
1.1 oop-klass model
Meta information of the oop object klass class
Look at the inheritance structure of the klass model class . It can also be seen from the inheritance relationship that the meta information of the class is stored in the original space (MetespaceObj).
1.1.1 InstanceKlass
InstanceKlass: The normal Java class corresponds to an instance of the instanceKlass class in the JVM. (Image understanding) The class loader loads the .class file into the JVM, parses the .class file, and stores the meta-information of the class in the JVM
Three subclasses of InstanceKlass:
- InstanceMirrorKlass (mirroring class): used to represent java.lang.Class, the Class object obtained in the Java code, which is actually an instance of this C++ class, stored in the heap area
- InstanceRefKlass: used to represent the subclasses of java/lang/ref/Reference, that is, four types of references, strong and weak
- InstanceClassLoaderKlass: used to traverse the classes loaded by a loader
1.1.2 ArrayKlass
ArrayKlass: Stores the meta-information of the array class. The meta information of Java arrays is represented by subclasses of ArrayKlass
- TypeArrayKlass: Represents the existence of basic types of arrays in the JVM.
- ObjArrayKlass: Represents the existence form of the reference type array in the JVM.
1.2 The process of class loading
1.2.1 Loading
1. Obtain the class file that stores the class through the fully qualified name of the class (package name + class name)
2. parse it into runtime data, that is, instanceKlass instance, and store it in the method area
3. Generate the Class object of the class in the heap area, That is, the instanceMirrorKlass instance
1.2.2 Verification
1. File format verification
2. Metadata verification
3. Bytecode verification
4. Symbol reference verification
1.2.3 Preparation
Allocate memory and assign initial values to static variables.
Instance variables are assigned when the object is created. There is no initial value assigned.
If it is modified by final, the ConstantValue attribute will be added to the attribute when compiling, and the assignment is completed directly in the preparation phase, that is, there is no initial value assignment step
1.2.4 Analysis
Convert symbol references in the constant pool to direct references
Simply put, it is to convert indirect references to direct references
Indirect reference: a reference to the runtime constant pool
Direct reference: memory address
1.2.5 Initialization
Execute the static code block to complete the assignment of static variables
detail:
- Static fields, static code segments, and clinit methods are generated at the bytecode level
- The order of the statements in the method is related to the order in which the code is written
1.3 When is the class loaded
JVM loading class is lazy loading
- new、getstatic、putstatic、invokestatic
- reflection
- Initializing a subclass of a class will load its parent class
- Start class (the class where the main function is located)