Java ClassLoader Parental delegation mechanism

Source: Internet
Author: User


First, look at the loader.


650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2015/0831/151224_tHEL_1990220.png "alt=" 151224 _thel_1990220.png "/>

1, Bootstrapclassloader: Start the class loader, the ClassLoader is created at startup time, is written in the JVM kernel , it is not a bytecode file, is written by C + + binary code, So the developer cannot get a reference to the startup class, and it cannot be manipulated by reference. This loader is the class library that is loaded $java_home/jre/lib (or specified by parameter-xbootclasspath).


2, Extclassloader: Extension class loader, Extclassloader load the class library under the $JAVA _home/jre/lib/ext (or specified by the parameter-djava.ext.dirs).


3. Appclassloader: The application loader will load the class library under the path specified by the Java environment variable classpath, and the path specified by Classpath can be Systemn.getproperty (" Java.class.path ") gets, the variable can be overwritten.


4, CustomClassLoader: Custom loader, is the user's own definition of classloader, such as Tomcat Standardclassloader belong to this category.


ClassLoader Parent delegation Mechanism:

1, when Appclassloader loads a class, it does not load the class itself first, but instead delegates the class load request to the parent ClassLoader Extclassloader to complete.

2, when Extclassloader load a class, it will not try to load the class first, but instead of delegating the class load request to Bootstrapclassloader to complete.

3, if the Bottstrapclassloader load fails, will use Extclassloader to try to load.

4, if the Extclassloader also failed to load, the Appclassloader will be used to load, if the Appclassloader also failed to load, will report an exception classnotfundexception.


The specific meaning and meaning of the delegation mechanism:

When Java virtual machines load a class, exactly which classloader to exclude from loading?

For example: the class loader of the current thread is going to load a class A in the thread, if the Class A references a class B,java the virtual machine will use the loader that loads class A to load Class B. When Class B is re-used, if it finds that Class B is loaded, it does not load the class B.


I think the point is to prevent multiple copies of the same bytecode from appearing in memory.

If you load yourself without a delegate, class A loads a copy of the system bytecode, and then class B loads a copy of the system bytecode so that two copies of the system bytecode are present in memory.

If you use a delegate mechanism, you will recursively find the parent class, which is preferred to try to load with bootstrap if it is not found again. The system here can be found in bootstrap and then loaded, and if Class B also loads the system at this point, starting with Bootstrap, At this point, Bootstrap found that the system has been loaded and returned directly to the in-memory system without reloading, so that only one copy of the system's bytecode is in memory.



This article from "10093778" blog, declined reprint!

Java ClassLoader Parental delegation mechanism

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.