This is the 17th day of my participation in Gwen Challenge
What is the classloading subsystem
The classloading subsystem is a system that manages the loading of.class files into memory. It is a sub-module of the JVM system, so it is called a subsystem. When a class needs to be loaded, the JVM calls the classloading subsystem directly
For example, the JVM is the owner of an apartment, and the Class loading subsystem is the door guard. All guests who want to enter the apartment (Class) need to pass through the door, and then the security guard needs to open the door
Second, the process of class loading
Diagram of the life cycle of the Class Class
1. The load
Loading is to read a. Class file from disk into memory through a byte stream, read it by the fully qualified name of the class (that is, the path in the folder), and store the data in the method area. So that’s a Class file information, and then the Class loader creates an object in the heap to point to that information, and that object is the object that we get reflected as the entry to the method area, and that’s the Class object
If you don’t already know what’s in the.class file, check out my article bytecodes. Loading is the only phase we can touch
2. Verify
Validation is to ensure that incoming Class file information does not harm the JVM system. There are four main types of verification
- File format verification. Verify that the JVM supports magic and version information for the Class file information, and that there are data types in the constant pool that the JVM does not support
- Metadata validation. Semantic analysis is performed on the information described by bytecode, such as whether the class has a parent class, whether it inherits classes that cannot be inherited, etc
- Bytecode verification. This validation is a syntactic validation of the remaining bytecode information
- Symbol reference validation. Verify that the fully qualified names of other classes in the Class file information are correct, that properties or methods that access other classes exist, and that access permissions match
3. Prepare
To prepare a static variable is to allocate memory in the method area and assign a default value. Static variables are properties with the static keyword
4. The parsing
Parsing is the process of converting symbolic references (references to data in the constant pool) in the Class file information into direct references, so that the Class file information is already similar to the decompile file
5. The initialization
Initialization is a call to the **() method ** in the Class file information. This method is generated by the compiler automatically collecting all Class variable assignments and static code blocks in the Class loader in the order of assignment followed by static code
Class when the Class is initialized
With Class Class Class Class initialization time of the loading time is very similar, but between them there is a sequence of, loading in the front of the initialization In addition to basic data classes, all other classes are not full loaded into memory from the start, a kind of want to be loaded into the JVM, only the active reference Class Class, to perform initialization, A passive reference to a Class does not initialize an active reference to a Class:
- When you encounter four bytecode instructions: New, getstatic, putsatic, or Invokestatic. In plain English, Class initialization is triggered when an object is new, a static property is modified, and a static method is called
- When a Class object is called through reflection. When a Class object is called through reflection, if it is not in memory, the Class loading subsystem is immediately asked to load and create the Class object
- When the instantiated object has a parent class. Initialization of the Class Class is triggered when an object is instantiated and the Class object of its parent is not found in memory
- When the main() method is run. When the main method is run, it initializes the class in which the main method resides actively
- When using a method handle. A MethodHandle creates a MethodType object based on the return type and parameter type, and then adds a.class and method to that object to create a MethodHandle object that calls the method of that class without creating the class. In this case, initialization of the class is also triggered
A passive reference to a Class:
- A static property of a parent class is invoked by a subclass. When you use a subclass to call a static property on a parent class, the loading of the subclass is not triggered, but the initialization of the parent class is triggered
- When defining an array of classes. When you create an array of objects, you do not initialize the class, because you actually store the address, not the class, in the array
- Reference class constants. Constants are properties that have the static final keyword
Using &unload is out of the hands of the classloader, but is controlled by the JVM
Class loaders
1. Class loader type
- Bootstrap ClassLoader
Jar, resource. Jar, or the jar package in the sun.boot.class.path path. The startup class loader does not have a parent class loader and is not inherited from the ClassLoader. This class is implemented in C/C++ and is nested within the JVM
- Extension ClassLoader
The extension ClassLoader is derived from (but similar to) the ClassLoader. Its parent is the launcher ClassLoader (not inherited, as you can see from the parent delegation mechanism). This class is implemented in Java
- System ClassLoader (Application ClassLoader)
It is responsible for loading the class packages in the ClassPath path, that is, the classes we wrote and the third party classes, whose parent is the extension class loader, which is implemented in Java
- Custom class loaders
We can do this by inheriting classLoaders, which are used to load classes that we’ve written and all we can touch are custom classloaders, everything else is defined by Java
2. Parent delegation mechanism
Before customizing class loaders, it is important to understand the parent delegate mechanism to familiarize ourselves with the relationship between classloaders and the three class loaders
Mechanism concept
That is, every class loader doesLet’s see if this class has been loadedIf yes, return directly without loading. If no, return directlyCall the parent method to find if the class has been loaded; The parent class throws an exception if it has been loaded, but not if it has notTry loading yourself first.If it can’t load it, the subclass loader will load itAnd then all the way down.
benefits
Through layer upon layer inquiry, the core classes can be prevented from being destroyed, tampered with, and loaded many times, effectively ensuring the stability of the class library
Source code analysis
The loadClass method of ClassLoader is truncated
protectedClass<? > loadClass(String name,boolean resolve)
throws ClassNotFoundException
{
/ / acquiring a lock
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
// First, check that the class is loadedClass<? > c = findLoadedClass(name);// If it is null, it is not loaded
if (c == null) {
long t0 = System.nanoTime();
try {
// Check whether there is a parent class loader
if(parent ! =null) {
// Have the parent class loader execute its loadClass method and repeat the process
c = parent.loadClass(name, false);
} else {
// Call the class loader to check if it has been loadedc = findBootstrapClassOrNull(name); }}catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// The parent class could not be found or loaded
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); }}if (resolve) {
// I don't know what to do
resolveClass(c);
}
returnc; }}Copy the code
— — — — — — — — — — — — — — —
The more you know, the more you don’t know.
If you have any questions about the content of this article, please comment directly or email me. If you think my writing is good, a “like” is also a sign of support
Shall not be reproduced without permission
Wechat search [programmer Xu Xiaobai], attention can be the first time to read the latest article.