JVM (1)-class loader and class loading process

1.1 Overall structure diagram

Insert picture description here

1.2 The role of the class loader subsystem

Class 1 Load subsystem responsible for loading class files from the file system , class files in the beginning of the file with a specific file identification.
2 ClassLoader is only responsible for the loading of class files.
3 The loaded class information is stored in the memory space of the method area .
Concept of method area:

存放运行时常量池信息:{
    
    
字符串的字面变量,数字常量}

1.3 The role of the ClassLoader role

Insert picture description here
1 The class file [Car.class] is stored in the local disk.
2 The class file is loaded into the JVM method area.
Step 1———(Transporter: ClassLoader)————>Step 2

1.4 Class loading process

Insert picture description here

1.4.1 Loading

1 Obtain the binary byte stream that defines this class through the fully qualified name of a class 2 Transform the static storage structure represented by this byte stream into the runtime data structure of the method area 3 Generate a java representing this class in memory . lang.Class object [reflection mechanism] , as the access entry for various data of this class in the method area

1.4.2 Linking

1 Verification:
Ensure that the information contained in the byte stream of the class file meets the requirements of the current virtual machine and ensure the correctness of class loading.
2 Preparation (prepare):
allocate memory for class variables and set the default initial value of the class variables [0]

注意:
这里不包括final修饰的static,因为final在编译的时候就会被分配了,准备阶段会显示的初始化。
这里也不会为实例变量分配内存,实例变量会随着对象一起分配到java heap中

3 Resolve:
The process of converting symbol references in the constant pool into direct references.

1.4.3 Initialization (Initialization)

Initialization is the process of executing the class constructor method <clinit >() .

The javac compiler automatically collects the assignment actions of all class variables in the class and merges them with the statements in the static code block .

If the class has a parent class, the JVM will ensure that the <clinit >() of the parent class is executed before the <clinit >() of the subclass is executed

Demo
Insert picture description here
note:
The assignment process of a:
1 The prepare phase of Linking: a = 0;
2 The clinit phase: a =1

1.5 Classification of class loaders

According to whether the ClassLoader is inherited, it is divided into: boot class loader and custom class loader.
The relationship between the upper and lower layers:
Insert picture description here

1.5.1 Bootstrap ClassLoader (Bootstrap ClassLoader)

1 C/C++ language used
2 Load java core library classes starting with java javax sun
3 No parent loader
4 Ca n’t be obtained directly

1.5.2 Extension ClassLoader

1Java language writing 2Inheriting the
abstract class ClassLoader 3The
parent loader is the startup class loader

1.5.3 System Class Loader (AppClassLoader)

1Java language writing 2Inheriting
ClassLoader 3The
parent loader is the extended class loader 4The
default class loader of the program

Extend the system class loader demo to the boot class loader
Insert picture description here

1.5.4 Custom class loader

Significance:
Isolate the loading class
Modify the loading method of the class
Extend the loading source
Prevent the source code leakage
Implementation steps:
1 Inherit the java.lang.ClassLoader class
2 Rewrite the findClass() method
Demo
Insert picture description here

1.6 ClassLoader

Insert picture description hereWays to obtain ClassLoader
Insert picture description here

1.7 Parental delegation mechanism (focus of interview)

The JVM loads the class files on demand. When loading the class file of a certain class, the JVM uses the parent delegation mechanism , that is, the request is handed over to the parent class for processing.

1.7.1 Working principle

Insert picture description here
The Demo
Insert picture description here
java.lang.String class is the core java class, which is loaded by the boot class loader, but there is no main method in the original javaAPI. Therefore, it highlights the advantages of the parent delegation mechanism: protecting program security and preventing core APIs from being tampered with.

Insert picture description here

1.8 Other important knowledge points

Two necessary conditions for judging whether two class objects exist in the same class
1 The complete class name of the class must be the same, including the package name
2 The ClassLoader that loads this class must be the same


Will the active and passive use of the class cause the initialization of the class to be the basis for classification?

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/111028833