Class loading mechanism and parent delegation model

Class loading mechanism and parent delegation model

class loading mechanism

The JVM loads the .class file into the memory (method area), parses and initializes the data, and finally forms a Java type that is directly used by the JVM.

Class loading needs to be divided into 7 stages, namely: loading, verification, preparation, parsing, initialization, use and unloading. In general, we only focus on the first 5 stages. The verification, preparation and parsing are collectively referred to as the connection phase.

5 stages

Loading : At this stage, the virtual machine needs to do three things:

  1. Get the binary byte stream defining this class by its fully qualified name (this action is done by the class loader);
  2. Convert the static storage structure represented by this byte stream into the runtime data structure of the method area;
  3. A java.lang.Class object representing this class is generated in memory as an access entry for various data of this class in the method area.

Verification : Verify that the class data information conforms to the JVM specification and is a valid bytecode file.

Preparation : Allocate memory space for class static variables and initialize (unrelated to the program, system initialization).

Resolution : Convert all symbolic references in the constant pool to direct references.

Initialization : Responsible for executing all static fields according to the operation specified by the program (assign values ​​to static variables and execute the contents of static code blocks).

There is no strict order of execution of the above phases, and they are usually executed in an interleaved manner.

Classification of class loaders

The virtual machine design team put the action of "getting the binary byte stream of this class through the name of a class's authority" in the class loading phase to the outside of the JVM, so that the program can decide how to obtain the required class and realize the The module of this code becomes the "class loader".

For any class, its uniqueness in the Java virtual machine needs to be established by the class that loads it and the class itself.

From the perspective of the JVM, there are only two different class loaders, one is the startup class loader, which is implemented in C++ and is part of the virtual machine; the other is the other class loader, which is implemented in the Java language and independent of the virtual machine. External, and all inherit from the abstract class java.lang.ClassLoader.

From the developer's point of view, class loaders can also be divided into a little more fine-grained:

Start the class loader

In the HotSpot virtual machine, the BootStrap ClassLoader is written in C++ and embedded in the JVM. It mainly loads all the classes in the directory, or loads the classes in the path specified JAVA_HOME/libby the option .-Xbootclasspath

Extended class loader

Extension ClassLoader inherits the ClassLoader class, implemented by sun.misc.Launcher$ExtClassLoader, and loads JAVA_HOME/lib/extall class libraries in the directory, or all class libraries in the path specified by the java.ext.dirs system variable.

application loader

Application ClassLoader is implemented by sun.misc.Launcher$AppClassLoader. Since this class loader is the return value type of the getSystemClassLoader() method in ClassLoader, it is also generally called the system class loader. It is responsible for loading the class library specified on the user's class path (ClassPath). If the program has not customized its own class loader, this is generally the default class loader in the program.

In addition to this, you can define your own classloader.

Parent delegation model

When a class loader receives a class loading task, it immediately delegates the task to its parent class loader until it is delegated to the top-level startup class loader. The child loader will only attempt to load itself if the parent class reports that it cannot complete the load request (the required class is not found in its search scope).

Role : The parent delegation model can ensure that classes executed with full names are loaded only once. And Java classes have a prioritized hierarchy along with their class loader.

The parental delegation model has no mandatory constraints and is the recommended way for classloader implementations by the Java designers. The implementation of parent and child classes is not through inheritance but through combination to reuse the code of the parent class.

The parent delegation model is very important to ensure the stable operation of Java programs. But its implementation is very simple, and the implementation code is concentrated in java.lang.ClassLoaderthe loadClass() method.

Record the notes from "In-depth Understanding of Java Virtual Machine" for your future review and reference

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326547757&siteId=291194637