JVM class loading mechanism ---- 2. jvm Loading Mechanism

Source: Internet
Author: User

JVM class loading mechanism ---- 2. jvm Loading Mechanism

The first phase of the class loading mechanism is:

 

1. Use a fully qualified Class name (package name and Class Name) to obtain the binary byte stream (Class file) that defines this Class ). You can use the jar package, war package, network retrieval, and JSP file generation methods.

2. Convert the static storage structure represented by this byte stream into the runtime data structure of the method area. Here we only converted the data structure and did not merge the data. (The method area is used to store loaded class information, constants, static variables, and runtime memory areas of compiled code)

3. Generate a java. lang. Class Object that represents this Class in the memory, which serves as the access portal for various data of this Class in the method area. This Class object is not specified in the Java heap memory. It is special. Although it is an object, it is stored in the method area.

 

The code block that implements the first job is called the class loader ".

 

The classloader is not only used to load classes, but also related to the class's "Equality" judgment, which is related to the return results of the Java "Equality" judgment method, only when the following conditions are met for determining the "equality" of the three classes can the two classes be determined to be equal.

1. Two classes come from the same Class file

 

 

 

 

2. The two classes are loaded by the same virtual machine.

3. The two classes are loaded by the same class loader.

 

Java "equal" judgment method:

1. Determine whether the reference of two instance objects points to the same instance object in the memory. Use the equals () method of the Class Object, obj1.equals (obj2 );
2. Determine whether the instance object is an instance object of a Class, interface or its subclass or sub-interface, and use the isInstance () method of the class Object, Class. isInstance (obj );

3. Determine whether the instance object is an instance of a class or interface, and use the instanceof keyword, obj instanceof class;
4. Determine whether a Class is another Class or its subclass or sub-interface. You can use the isAssignableFrom () method of the Class Object, class1.isAssignableFrom (class2 ).

 

JVM class loaders:

1. Bootstrap ClassLoader: starts the class loader, also known as the root class loader. It loads the core class library of Java and loads rt in the directory (% JAVA_HOME %/lib. jar (including core classes such as System and String. The root loader is very special. It is not a subclass of java. lang. ClassLoader. It is implemented by C/C ++ In the JVM itself, and is not implemented by Java.

2. Extension ClassLoader: the Extension class loader that loads the jar package under the Extension directory (% JAVA_HOME %/jre/lib/ext, you can package your own classes into a jar package and put them in this directory to extend new functions other than the core classes.

3. System ClassLoader \ APP ClassLoader: The System class loader, or application class loader, is the jar package and class path specified to load the CLASSPATH environment variable. Generally, user-defined classes are loaded by APP ClassLoader.

 

Relationship between various types of loaders: the parent-child relationship of the parent-child loader is reused by the combination relationship. Note that the parent-child relationship here is not implemented by the inheritance relationship.

 

[Java]View plain copy
  1. // Verify the parent-child relationship between the class loader and the Class Loader
  2. Public static void main (String [] args) throws Exception {
  3. // Obtain the system/Application Class Loader
  4. ClassLoader appClassLoader = ClassLoader. getSystemClassLoader ();
  5. System. out. println ("System/Application Class Loader:" + appClassLoader );
  6. // Obtain the parent class loader of the system/Application class loader to obtain the extension class loader.
  7. ClassLoader extcClassLoader = appClassLoader. getParent ();
  8. System. out. println ("extension class loader" + extcClassLoader );
  9. System. out. println ("extension class loader loading path:" + System. getProperty ("java. ext. dirs "));
  10. // Obtain the parent loader of the extended Class Loader. However, the root class loader cannot be obtained because it is not implemented in Java.
  11. System. out. println ("parent class loader of the extension class:" + extcClassLoader. getParent ());
  12. }
  13. }

 

Parent-parent delegation loading mechanism of the Class Loader (important): When a class receives a class loading request, it will not try to load the class by itself first, instead, this request is delegated to the parent class to complete. This is true for each layer Class Loader. Therefore, all loading requests should be transmitted to the startup class for loading, only when the parent Class loader reports that it cannot complete this request (the Class to be loaded is not found in its loading path) Will the subclass loader attempt to load it by itself.

 

This process is shown in the labeling process:

 

 

Source code implementation of the parent-child Delegation Model:

It is mainly reflected in the loadClass () method of ClassLoader. The idea is simple: first check whether it has been loaded. If it is not loaded, call the loadClass () method of the parent class loader, if the parent class loader is empty, the start class loader is used as the parent class loader by default. If the parent classloader fails to load, the ClassNotFoundException exception is thrown, and the findClass () method is called to load the object.

Source code implementation of the parent-child Delegation Model:

It is mainly reflected in the loadClass () method of ClassLoader. The idea is simple: first check whether it has been loaded. If it is not loaded, call the loadClass () method of the parent class loader, if the parent class loader is empty, the start class loader is used as the parent class loader by default. If the parent classloader fails to load, the ClassNotFoundException exception is thrown, and the findClass () method is called to load the object.

 

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

 

 

Let's take a look at a simple example of the parent-parent Delegation Model Code:

 

[Java]View plain copy
  1. Public class ClassLoaderTest {
  2. Public static void main (String [] args ){
  3. // Name of the classloader that outputs ClassLoaderText
  4. System. out. println ("Name of the ClassLoaderText class Loader:" + ClassLoaderTest. class. getClassLoader (). getClass (). getName ());
  5. System. out. println ("Name of the System class Loader:" + System. class. getClassLoader ());
  6. System. out. println ("Name of the listclass Loader:" + List. class. getClassLoader ());
  7. ClassLoader cl = ClassLoaderTest. class. getClassLoader ();
  8. While (cl! = Null ){
  9. System. out. print (cl. getClass (). getName () + "-> ");
  10. Cl = cl. getParent ();
  11. }
  12. System. out. println (cl );
  13. }



 

 

Output result:

Explanations:

1. The ClassLoaderTest class is a user-defined class. It is located under CLASSPATH and loaded by the system/Application class loader.

2. Both the System class and List class belong to the Java core class, which is loaded by the class loader started from the ancestor class, and the class loader started through C/C ++ In the JVM, if it is not Java, the ClassLoader class cannot be inherited and its name cannot be output.

3. arrows represent the class loading process. Hierarchical delegation starts from the ancestor class loader and is not loaded until the system/Application class loader.

 

So let's perform a test, compress the class into a jar package, copy it to the % JAVA_HOME %/jre/lib/ext directory, and run the ClassLoaderTest class again.

To explain, because the Jar package of the class is placed in the directory where ExtClassLoader is loaded, after the corresponding class cannot be found in the root directory, the class is loaded at ExtClassLoader, the APPClassLoader stage is ignored.

Reprinted, original connection: http://blog.csdn.net/zhangliangzi/article/details/51338291

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.