05JVM_Class loading phase

1. Class loading stage

1.Load

1.1 Introduction

①The Java source code is compiled to generate a bytecode file , and the bytecode is loaded into the method area through the class loading stage .

②In the class loading phase, C++ instanceKlass describes the java class. The important fields are:

_java_mirror,java类镜像。例如对String来说,就是String.class,作用把klass暴露给java使用。【桥梁】
_super 父类
_fields 成员变量
_methods 方法
_constants 常量池
_class_loader 类加载器
_vtable 虚方法表
_itable 接口方法表

③The class has a parent class, load the parent class first.

④Loading and linking may run alternately

1.2 Note

① The loaded class creates a corresponding class structure in the JVM. The metadata of instanceKlass is stored in the method area (jdk1.8 is in the metaspace), but the java class mirror of _java_mirror is stored in the heap.

②After loading the .class file into the metaspace, a java.lang.Class object will be created in the heap. The Class object is created during the process of loading a class. Each class has an object of Class type.

③Externally obtain the Person class data structure by accessing the Class object of the Person class. Through the interface provided by the class class, you can obtain the specific data structure of the .class file associated with the target class: methods and field information.

[Illustration]

The class is loaded into the metaspace of the method area , the bytecode of the class is loaded into the metaspace to form the instanceKlass data structure, and the java class mirror Person.class class object of _java_mirror is generated in the heap memory.

②When new creates an instance object of Person . The object header of each instance is stored corresponding to the class address . The Person.class class object can be found through this class address , and then the instanceKlass is found indirectly in the metaspace to obtain the methods and field information of the Person class.

2. Links

Three stages of linking

Validate, prepare, parse.

2.1 Link-Verification

1. Purpose of verification

Verify whether the class complies with JVM specifications and perform security checks.

2.1 Link-Preparation

① Allocate space for static variables and set default values.

②After jdk1.8, static variables are stored behind heap space class objects

③Static variables before jdk6 in the early days were stored behind instanceKlass in the method area.

④The allocation of space for static variables is completed in the preparation phase , and the assignment is completed in the initialization phase.

⑤static final basic type variable. Allocate space and assign values ​​in the preparation phase. If it is a reference type , assignment occurs during initialization.

2.1 Link-Resolution

The meaning of analysis:

Resolve constant pool symbol references to direct references. Get the address of the class, field, and method in memory.

3.Initialization

Initialize and assign values ​​to static variables declared in the class

3.1<cinit>()V method

Initialization calls <cinit>()V , and the virtual machine ensures the thread safety of the constructor of this class.

3.2 Timing of occurrence

Class initialization is lazy

Class initialization

① The class where the main method is located is initialized first. as the entry point of the program

②When accessing static variables or static methods of this class for the first time

Subclass initialization. If the parent class is not initialized, it will trigger parent class initialization.

④ When a subclass accesses the static variables of the parent class, only the parent class initializes them.

⑤Reflection, Class.forName

⑥new will cause initialization

Class cannot be initialized

①Accessing the static final static constants (basic types and strings) of the class will not initialize the class. (Preparatory phase completed)

②Class object.class will not trigger initialization

③Creating an array of a class will not trigger initialization

④The loadClass method of the class loader will only load, but not parse and initialize.

⑤Parameter 2 of Class.forName is false

practise

1. Based on bytecode analysis, will using the three constants a, b, c cause E to be initialized?

Ea and Eb will not , they are static final basic type variables. Assignment in preparation phase

Ec Yes, static final reference type variables must be initialized for assignment .

2. Complete lazy initialization singleton mode

 Lazy instantiation, initialization thread safety is guaranteed.

Guess you like

Origin blog.csdn.net/jbkjhji/article/details/132845260