Chatting about the class loading process

Talk about the class loading process, follow-up to continue to add

 

Class loading process: loading, verification, preparation, analysis, initialization, use, unloading

Verification, preparation, and analysis can be collectively referred to as connection

Loading :

1) Obtain the binary byte stream of the class through the fully qualified name of the class; 

2) Convert the static storage structure represented by the byte stream into the runtime data structure of the method area; 

3) Generate a java.lang.Class object of this class in memory as the access entry for various data of this class in the method area.

 

Verification :

1) File format verification 

2) Metadata verification (whether it conforms to the Java language specification) 

3) Bytecode verification (make sure the program semantics are legal and logical) 

4) Symbol reference verification (to ensure that the next step of parsing can be executed normally)

 

Prepare :

Allocate memory in the method area for static variables and set the default initial value

note

At this time, the memory allocation only includes the class variable static, not the instance variable. The instance variable will be allocated in the java heap along with the object when the object is instantiated.

The default value is set, such as int=11, at this time the initial value is 0, and 11 is initialized before assignment

 

Analysis :

The process of converting virtual references in the constant pool into direct references

 

Initialization :

Actively assign values ​​to class variables according to assignment statements in the program

Note: There is a parent class that initializes the parent class first, and then initializes the subclass

 

The ClassLoder in the system uses the parent delegation model by default when working together. That is, when the class is loaded, the system will first determine whether the current class has been loaded, the loaded class will return directly, otherwise it will try to load

 

Parental Delegation Model

The working process is: if a class loader receives a request for class loading, it will not load the class first, but will delegate the request to the parent loader above it. This is the case for each layer, so the final request will be passed to Start the class loader, and then try to load the class from the start of the class loader. If it can't be loaded (the class to be loaded is not in the loading range of the current class loader), let its subclasses try to load. This is the case for each layer.

benefit:

Avoid repeated loading of classes

Ensure that the core API of java is not tampered with

 

 

Guess you like

Origin blog.csdn.net/Goligory/article/details/104467805