Hot Spot recommendation: deep understanding of Java class loaders

Source: Internet
Author: User

Hot Spot recommendation: deep understanding of Java class loaders

First, let's review the steps for loading a java class on the java VM: After the java file is compiled by the compiler, it becomes a bytecode file. class file), the class Loader (ClassLoader) read. class file and convert it to java. lang. an instance of the Class, and finally an object of the Class is created through the newInstance method. ClassLoader is used to locate the corresponding bytecode based on a Class name and define the corresponding Class based on these bytecode. This Class is an instance of java. lang. Class.

Organizational structure of the Class Loader

Java has three initialization class loaders. When the java Virtual Machine starts, they start in the following order: Bootstrap classloader-> extension classloader-> system classloader. Relationship: bootstrap classloader is the parent of extension classloader, and extension classloader is the parent of system classloader.

Bootstrap classloader

It is the most primitive class loader, not written by java code, but written by native code. Java once compiled and runs on all platforms because it writes the same underlying code for different languages on different platforms. It is responsible for loading the java core library. You can run the following code to see where your local java core library is:

 
  1. URL[] urls=sun.misc.Launcher.getBootstrapClassPath().getURLs(); 
  2. for (int i = 0; i < urls.length; i++) { 
  3. System.out.println(urls[i].toExternalForm()); 
  4. } 

My running result:

 
  1. file:/home/eric/jdk1.6.0_35/jre/lib/resources.jar 
  2. file:/home/eric/jdk1.6.0_35/jre/lib/rt.jar 
  3. file:/home/eric/jdk1.6.0_35/jre/lib/sunrsasign.jar 
  4. file:/home/eric/jdk1.6.0_35/jre/lib/jsse.jar 
  5. file:/home/eric/jdk1.6.0_35/jre/lib/jce.jar 
  6. file:/home/eric/jdk1.6.0_35/jre/lib/charsets.jar 
  7. file:/home/eric/jdk1.6.0_35/jre/lib/modules/jdk.boot.jar 
  8. file:/home/eric/jdk1.6.0_35/jre/classes 

Extension classloader

It is used to load the JAR class package specified by JAVA_HOME/JRE/lib/ext or java. ext. dirs system attribute. Note that because it is loaded by bootstrap classloader, when you run:

ClassLoader extensionClassloader = ClassLoader. getSystemClassLoader (). getParent ();
System. out. println ("the parent of extension classloader:" + extensionClassloader. getParent ());

The output is: the parent of extension classloader: null

System classloader

It is used to load the jar package under the classpath directory. the java class we write is usually loaded by it unless you have made your own class loader.

Overall entrusting Mechanism

When a classloader loads a class, it uses the full-responsibility delegation mechanism. It can be understood in two parts: Full-responsibility and delegation.

Full responsibility mechanism: if Class A calls Class B, all jar packages introduced by class B and class B are uniformly loaded by the class loaders of Class.

Delegation mechanism: when the class loader loads Class A, it first loads the parent loader. If the parent loader fails to load, it will find the parent loader again, the bootstrap classloader cannot be found until it finds the relevant path for loading. The following is the source code of ClassLoader:

 
  1. Protected synchronized Class <?> LoadClass (String name, boolean resolve)
  2. Throws ClassNotFoundException
  3. {
  4. // First, check if the class has already been loaded
  5. Class c = findLoadedClass (name );
  6. If (c = null ){
  7. Try {
  8. If (parent! = Null ){
  9. // Load from the parent Loader
  10. C = parent. loadClass (name, false );
  11. } Else {
  12. // Load from bootstrap loader
  13. C = findBootstrapClassOrNull (name );
  14. }
  15. } Catch (ClassNotFoundException e ){
  16. // ClassNotFoundException thrown if class not found
  17. // From the non-null parent class loader
  18. }
  19. If (c = null ){
  20. // If still not found, then invoke findClass in order
  21. // To find the class.
  22. C = findClass (name );
  23. }
  24. }
  25. If (resolve ){
  26. ResolveClass (c );
  27. }
  28. Return c;
  29. }

For example, the classloader loads Class:

1. Check whether it has been loaded and search in the cache. If yes, skip 7; otherwise, go to the next step.

2. Determine whether the current loader has a parent loader. If no parent loader exists, the current loader is ext classloader and the next step is skipped.

3. Request the parent loader to load the class. If the loading is successful, skip 7. If the loading fails, that is, the parent loader cannot find the class. Skip 2.

4. Request the jvm bootstrap classloader to load. If the load is successful, skip 7. If the load fails, skip 5.

5. The current loader is loaded by itself. If it succeeds, it will jump to 7. Otherwise, it will jump to 6.

6. Throw ClassNotFoundException.

7. Return Class

Compile your own class loader

The process of loading classes in Java is essentially to call the loadClass () method. In loadClass, The findLoadedClass () method is called to check whether the class has been loaded, if no, the loadClass () of the parent loader is called. If the parent loader cannot load the class, the findClass () is called to find the class.

Therefore, we need to create a new MyClassLoader to inherit java. lang. ClassLoader and override the findClass () method. It is mainly to redesign the scheme of searching for bytecode files, and then call definedClass to return the results.

I wrote a demo and used my class loader to load the specified java file with hot deployment effect. For details, see the following url.

Demo address: http://git.oschina.net/ericquan8/hot-deploy

 

 

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.