In-depth JVM (4): ClassLoader knowledge

Source: Internet
Author: User

1. What is ClassLoader?

A Java program is not a native executable file, but composed of many independent class files. Each file corresponds to a Java class. in addition, these class files are not immediately loaded into the memory, but are loaded into the memory as needed by the program. ClassLoader is responsible for loading class files to the memory.

Ii. Architecture of ClassLoader?

 

We can see the parent-child relationship between the class loaders (Note that it is not the set inheritance relationship of the class.) And jurisdiction.

(1) BootStrap is the top-level class loader, which is compiled by C ++ and has been embedded into JVM, it is mainly used to read the Java core class library JRE/lib/rt. jar

(2) Extension ClassLoader is used to read Java Extension class libraries and read JRE/lib/ext/*. jar

(3) App ClassLoader is a class file used to read all jar packages or Directories specified by CLASSPATH.

(4) M ClassLoader is customized and used to read specified class files

3. What is a two-parent delegation model?

The model of parental appointment can be reflected through the following process:
(1) When "Class A loader" loads A class, first judge whether the class has been loaded,
(2) if it has not been loaded, first delegate its "Class A loader"'s "parent loader" to load the class, which is A process of continuous upward search, if all the "parent class loaders" (including bootstrap classloader) of Class A are not loaded to the class, the Class A loader is returned to the initiator, then, ClassNotFoundException is thrown.

For more information, see the loadClass (String name, boolean resolve) method of java. lang. ClassLoader. The code is summarized as follows:

// First, check if the class has already been loaded Class c = findLoadedClass (name); if (c = null) {try {if (parent! = Null) {c = parent. loadClass (name, false);} else {c = findBootstrapClass0 (name);} catch (ClassNotFoundException e) {// If still not found, then invoke findClass in order // to find the class. the findClass method here should be overwritten. By default, it directly throws ClassNotFoundException c = findClass (name );}
 

How can we verify this model? Let's look at the following program to find out the ClassLoaderTest class loader and all its ancestor loaders.

package com.classloader.test;public class ClassLoaderTest {    public static void main(String[] args) {        ClassLoader loader = ClassLoaderTest.class.getClassLoader();                   while (loader != null) {            System.out.println(loader.getClass().getName());            loader = loader.getParent();        }        System.out.println(loader);    }}
The running result is as follows:
 
The first row indicates that the ClassLoaderTest class loader is AppClassLoader.
The second row indicates that the class loader of AppClassLoader is ExtClassLoder.
The result in the third row indicates that null indicates that the class loader of ExtClassLoader is Bootstrap ClassLoader.
 
Then, we can compress this program into the jar package ClassLoaderTest. jarJRE \ lib \ ext \Directory, and then run the program again.
The result is as follows:
 
 
Why are different results?
Because the loading of Java classes satisfies the parent-parent delegation principle, when I load the ClassLoaderTest class, first check whether the current class loader has loaded this class. If not
If the class loader at the upper level loads the class, it will return directly if it can be loaded.
ExtClassLoader will automatically load all jar packages under JRE \ lib \ ext. So when we put ClassLoaderTest under it, we will use it when the program runs.
When the parent load class ExtClassLoader is loaded, it will find that com. classloader. test. ClassLoaderTest has been loaded, so the current program runs
Only two class loaders are used: ExtClassLoader and BootStrap.
 
UseJava-verbose: class com. classloader. test. ClassLoaderTestYou can see more specifically the class loading process during running, such:
 
 
PS:
Download ClassLoaderTest. jar.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.