JVM and the like loader-"Crazy Java Handout"

Source: Internet
Author: User

1. Introduction to Class Loaders

The ClassLoader is responsible for loading all classes, and the system generates an Java.lang.Class instance for all classes that are loaded into memory. Once a class is loaded into the JVM, the same class is not loaded again. Now the question is how to count "The same class"? Just as an object has a unique identity, a class that is loaded into the JVM also has a unique identity.

Similarly, classes loaded into the JVM also have a unique identity, in Java, a class is identified with its fully qualified class name, including the package name and class name. In the JVM, however, a class uses its fully qualified class name and its class loader as its unique identity. Therefore, if there is a class named person in the PG package that is loaded by the instance KL load of the ClassLoader Klassloader, the class object corresponding to the man class is represented in the JVM as (person, PG, KL). This means that two class loaders load classes with the same name: (person, PG, KL) and (person, PG, KL2) are different. The classes they load are also completely different and incompatible.

When the JVM starts, it forms an initial class loader hierarchy consisting of three classloader:

  • Bootstrap ClassLoader: Root class loader.

  • Extension ClassLoader: Extension class loader.

  • System Classloaser: Class loader.

    Bootstrap ClassLoader, called Boot (also known as primitive or root) classloader. It is responsible for loading the core classes of Java. In Sun's JVM, you can specify to load additional classes by using the-xbootclasspath option when executing java.exe commands or by specifying Sun.boot.class.path system property values using the-D option.

    The root ClassLoader is very special, it is not a subclass of Java.lang.ClassLoader, but is implemented by the JVM itself.

    Extension ClassLoader, known as the Extension class loader, is responsible for loading the class package of the jar in the JRE's extended directory (Java_home/jre/lib/ext or the directory specified by the Java.ext.dirs system properties).

    System Classloaser, known as the systems (also called application) ClassLoader, is responsible for when the JVM starts, Loads the-classpath option from the command Java or Java.class.path system property, or classpath the jar package and classpath specified by the environment variable.

  • 2. Class loading mechanism

The class loading mechanism of the JVM consists of the following three mechanisms:

    • Overall responsibility: That is, when a classloader loads a class, the class lock-dependent and referenced other classes will also be loaded by that ClassLoader load unless explicitly loaded with a different classloader.

    • Parent class Delegate: The first time the parent class loader attempts to load the class, it is only attempted to load it from its own classpath when it is loaded by the parent class loader midday.

    • Caching mechanism: The caching mechanism will guarantee that all classes loaded by the class will be cached, and when a class is needed in the program, the ClassLoader will first search for the class from the cache, and only the class object does not exist in the cache, and the binary data corresponding to that type is reread. and convert it into a class object and store it in the cache. This is why the program must restart the JVM after the class has been modified, and the changes made by the program will not take effect for the reason

JVM and the like loader-"Crazy Java Handout"

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.