A picture takes you to understand the class loading subsystem

 

1 Overview

1.1 What is it : Part of the JVM

1.2 What to do : Responsible for loading classes, verifying classes, initializing classes, and constructing bytecode objects of classes

2. Class loader

What is 2.1 : the object in the class loading subsystem responsible for reading the class into memory

2.2 What class loaders are there:

        2.2.1 BootStrapClassLoader

        2.2.2 ExtClassLoader

        2.2.3 AppClassLoader

        2.2.4 Custom Class Loader

2.3 Class loading mechanism - parent delegation model :

        Advantages : It can be guaranteed that it can only be loaded once by the same class loader, and at the same time ensure the robustness of the class system (for example, the java.lang.Object class written by ourselves cannot be loaded)

        Disadvantages : 1) It may have some impact on efficiency; 2) It may not be possible to achieve correct class loading for classes with the same class name and package name in different projects

3. Class loading method

3.1 Explicit loading : just call the method of the class loader directly to load the class

3.2 Implicit loading : 1) Access to class members (static properties or methods)

                     2) Construct the object of the class (new)

4. Class loading process

4.1 Loading

        4.1.1 Finding classes : Different class loaders have different class loading paths

        4.1.2 Read class : The bottom layer will call io through the thread to read the class on the disk or network into the memory

4.2 Connection

        4.2.1 Verification : verify the validity of structural information

        4.2.2 Preparation : Default initialization of class variables

        4.2.3 Analysis : Convert some symbolic references to direct references: for example, which constant in the constant pool is accessed, which method in which class is called

4.3 Initialization class

1) Initialize and assign values ​​​​to class variables (for example, static int a =10, the preparation stage is 0, and the initialization stage is 10)

2) Execute the static code block of the class

        4.3.1 Active use : Passive use will not initialize the class

        4.3.2 Passive use

5. Custom class loader

5.1 Why : The default class loader does not meet our needs

5.2 How to customize : directly or indirectly inherit the ClassLoader class and rewrite related methods

Guess you like

Origin blog.csdn.net/weixin_72125569/article/details/126861138