preface
This article takes you to explore the class loading mechanism of JVM and the two parent delegation mechanism
1, Class loader loading process
Class loading process will go through:
The loading process of class loader includes loading, verifying, preparing, parsing, initializing, using and unloading
Analysis of each stage:
- Load: find the bytecode file on the hard disk and read it through IO, and generate the Java of this class at the load node class. Class object
- Verification: verify the accuracy of bytecode file
- Resolution: replace symbolic reference with direct reference
- Initialization: initialize the static variable of the class to the specified value and execute the static code block
#2, Classification of class loaders
- Bootstrap loader: when loading the classes required by the JVM, $Java will be added_ The bottom layer of the file under home / JRE / lib is implemented in C language
- Extension loader: by sun misc. Launcherextclassloader is implemented, and it will load Java_ Files in the home / JRE / lib / ext directory (or files specified by System.getProperty("java.ext.dirs"). The bottom layer is java implementation
- Appclassloader: by sun misc. Launcher $appclassloader implementation. The class and jar package under classpath will be loaded. The bottom layer is java implementation
- Custom loader
3, Parental delegation mechanism
When our class loader receives a request, we will first look up the class loader without parent class at the top level in turn (start the class loader) and read the class file down in turn. If the class loader has read the class file, the child nodes will not continue to read it
4, Parent delegation source code analysis
- First, check whether the class with the specified name has been loaded. If it has been loaded, you don't need to load it again and return directly
- If this class has not been loaded, then judge whether there is a parent loader; If there is a parent loader, it will be loaded by the parent loader (that is, call parent.loadClass(name, false);) Or call the bootstrap class loader to load
- If neither the parent loader nor the bootstrap class loader can find the specified class, call the findClass method of the current class loader to complete the class loading
//The loadClass method of ClassLoader implements the parental delegation mechanism protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { // Check whether the current class loader has loaded the class Class<?> c = findLoadedClass(name); if (c == null) { long t0 = System.nanoTime(); try { if (parent != null) { //If the parent loader of the current loader is not empty, delegate the parent loader to load the class c = parent.loadClass(name, false); } else { //If the current loader parent loader is empty, delegate the boot class loader to load the class c = findBootstrapClassOrNull(name); } } catch (ClassNotFoundException e) { // ClassNotFoundException thrown if class not found // from the non-null parent class loader } if (c == null) { // If still not found, then invoke findClass in order // to find the class. long t1 = System.nanoTime(); //Will call the findClass method of URLClassLoader to find and load the class in the classpath of the loader c = findClass(name); // this is the defining class loader; record the stats sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); } } if (resolve) { //Will not execute resolveClass(c); } return c; } }
5, Benefits of parental delegation mechanism
In order to prevent the conflict between the class defined by the developer and the source class defined by jdk, and ensure the uniqueness of this class in memory
6, How to break parental delegation
- The custom class loader overrides the loadClass method
- The Spi mechanism bypasses the loadClass method. Set the associated class loader for the current thread
summary
This paper mainly introduces the loading process and classification of class loader, the principle and analysis of parental delegation mechanism, etc