JVM — the class loading subsystem

@[toc]

Class loading subsystem

Class loading process

loading
  • Get the binary stream that defines a class by its fully qualified name,

  • Converts the static storage structure represented by the class byte stream into the runtime data structure of the method

  • A Class object representing the Class is generated in the == heap area of memory, which serves as an access point to the data structure of the Class in the method area

Distinguish between two classes that are the same object

First, whether the two classes are in the same package, whether the same name

Whether the loaders of the two classes are the same

Mode of loading

  • Load directly on the local system
  • Obtain over the network
  • Read from the ZIP package
  • Runtime computational generation, implemented through dynamic proxies
  • Generated by other files
  • Extract.class files from a proprietary database
  • Obtained from the encrypted file
link

validation

Ensure that the information contained in the byte stream of the Class file meets the vm requirements. Ensure that the Class is correctly loaded, and the weihai VM is not safe.

There are four types of validation: file format validation, metadata validation, bytecode validation, and symbolic reference validation.

To prepare

==final static variables are not allocated because final variables are allocated at compile time and are explicitly initialized during preparation; Class variables are allocated to the method area, and instance variables are allocated to the Java heap along with the object.

parsing

The process of converting a symbolic reference in a constant pool to a direct reference

It is executed after the JVM completes the initialization

Initialize the

Static code blocks perform initialization operations on static variables of the class

Initialization is the process of executing a class constructor method by executing the Javac compiler to automatically collect all == class variables == and assignment actions and statements in static code blocks.

The virtual machine must ensure that the execution class constructor methods are executed only once, and in a multi-threaded environment, are locked synchronously.

When a class is initialized:

1) Create an instance of the class, i.e. new as an object

2) Access a static variable of a class or interface, or assign a value to the static variable

3) Call the static methods of the class

4) reflection (class.forname (“com.lyj.load”))

5) Initializing a subclass of a class (initializing the subclass’s parent class first)

6) When the JVM starts, that is, the name of the class is the same as the name of the class.

Class loader classification

Bootstrap ClassLoader :c, C ++ implementation

Failed to obtain. Not written in Java, nested within the JVM; But can be found which API for loading path: sun, misc. The Launcher. GetBootstrapClassPath () getURLS ();

Load extension classes and application class loaders and formulate them as their parent class loaders

Load only classes in directories whose package names start with Java, Javax, or Sun

Custom class loaders: Java implementation

Any class that directly or indirectly inherits from a ClassLoader is called a custom ClassLoader

Custom classes are loaded by default using the system class loader. The core class libraries of the system are loaded by the bootstrap class loader.

Extend the class loader

Derived from the classloader class, the parent classloader is the startup classloader

The class libraries are loaded from the JRE /lib/ext subdirectory of the JDK installation directory or from the directory specified by the java.ext.dirs system properties. If the jar package created by the user is placed in this directory, it will also be automatically loaded by the extended class loader

System class loader

Derived from classLoader, the parent class is the extension classloader

Responsible for loading libraries in the path specified by the environment variable classpath or the system property java.class.path

The class loader is the default class loader in a program. Generally speaking, it is the class loader that loads the Java application

User-defined class loaders

What are the cases where you need a custom classloader?
  • Isolated loader
  • Modify the class loading mode
  • Extended load source
  • Prevent source leakage
Steps to customize the loader?
  • Inherits the abstract classLoader

  • Override the findClass() method to write the custom logic in it

How to get the class loader

1. Obtain the IP address through reflection

Class.forName("java.lang.String").getClassLoader();

2. Obtain the data from a thread

Thread.currentThread().getContextClassLoader();

3. Obtain the system loader

ClassLoader.getSystemClassLoader().getParent();

Parent delegation mechanism

What does the parent delegation mechanism do?

If a classloader receives a load request, it does not load it itself. Instead, it passes it up to its parent class loader, and if the parent class still exists, it passes it up until it is handed over to the starting classloader. If the superclass loader can complete the load, it returns success, otherwise the subclass will attempt to load.

You can prevent the core class from being tampered with, and you can avoid reloading

Execution process:

1. When the system classloader receives a class loading request, it does not first attempt to load the class itself. Instead, it delegates the request to the parent classloader extension classloader.

2. When the extended classloader receives a class loading request, it does not first attempt to load the class itself. Instead, it delegates the request to the parent classloader to start the classloader.

3. If the startup class loader fails to load (the required class is not found in <JAVA_HOME>\lib), the extension class loader will try to load.

4. If the extension classloader also fails to load, the system classloader will be used to load.

5. If the system class loader also fails to load, the custom loader will be used to try to load.