Home  >  Article  >  Java  >  Analyzing the loading and unloading mechanism of Java classes from the JVM

Analyzing the loading and unloading mechanism of Java classes from the JVM

高洛峰
高洛峰Original
2017-01-13 09:35:511340browse

Analyzing the loading and unloading mechanism of Java classes from the JVM

Class loading
Class loading refers to the method of reading the binary data in the .class file of the class into the memory and placing it in the runtime data area. area, and then create a java.lang.Class object in the heap area to encapsulate the data structure of the class in the method area.

Ways to load .class files:

1. Load directly from the local system

2. Download .class files through the network

3. From Load .class files from zip, jar and other archive files

4. Extract .class files from proprietary database

5. Dynamically compile Java source files into .class files

The final product of class loading is the Class object located in the heap area.

The Class object encapsulates the data structure of the class in the method area and provides Java programmers with an interface to access the data structure in the method area.

Analyzing the loading and unloading mechanism of Java classes from the JVM

Class loader
There are two types of loaders:

1. The loader that comes with the Java virtual machine

Root class loader (Bootstrap)

Extension class loader (Extension)

System class loader or application loader (System)

The latter two loaders are Implemented in Java, the root class loader is written in C++, and programmers cannot obtain this class in Java code.

2. User-defined class loader

Subclass of java.lang.ClassLoader

Users can customize the loading method of classes

Class loading The compiler does not need to wait until a class is actively used for the first time before loading it.

The JVM specification allows the class loader to pre-load a class when it is expected to be used. If a .class file is missing or has an error during the pre-loading process, the class loader must load it for the first time in the program. An error (LinkageError) is reported only when this class is actively used. If the class has not been actively used by the program, the class loader will not report an error.

Class unloading mechanism
Class life cycle
When the Sample class is loaded, connected and initialized, its life cycle begins.

When the Class object representing the Sample class is no longer referenced, that is, when it is inaccessible, the Class object will end its life cycle, and the data of the Sample class in the method area will also be unloaded, thus ending the life of the Sample class. cycle.

It can be seen that when a class ends its life cycle, it depends on when the Class object representing it ends its life cycle.

Reference relationship
Loader and Class object:

In the internal implementation of the class loader, a Java collection is used to store references to the loaded classes.

On the other hand, a Class object always references its class loader. Call the getClassLoader() method of the Class object to obtain its class loader.

It can be seen that there is a two-way relationship between the Class instance and the loader that loads it.

Class, Class object of class, instance object of class:

An instance of a class always refers to the Class object representing the class.

The getClass() method is defined in the Object class. This method returns a reference to the Class object that represents the class to which the object belongs.

In addition, all Java classes have a static attribute class, which refers to the Class object representing this class.

Unloading of classes
Classes loaded by the class loader that comes with the Java virtual machine will never be unloaded during the life cycle of the virtual machine.

As introduced before, the class loaders that come with the Java virtual machine include root class loaders, extension class loaders and system class loaders.

The Java virtual machine itself will always refer to these class loaders, and these class loaders will always refer to the Class objects of the classes they load, so these Class objects are always reachable.

Classes loaded by user-defined class loaders can be unloaded.

Specific example

Analyzing the loading and unloading mechanism of Java classes from the JVM

The loader1 variable and the obj variable indirectly apply the Class object representing the Sample class, while the objClass variable directly refers to it.

If the three reference variables on the left side of the above figure are all set to null while the program is running, the Sample object will end its life cycle, the MyClassLoader object will end its life cycle, and the Class object representing the Sample class will also end its life cycle. , the binary data of the Sample class in the method area is unloaded.

When needed again, it will check whether the Class object of the Sample class exists. If it exists, it will be used directly without reloading; if the Sample class does not exist, it will be reloaded and a new one will be generated in the heap area of ​​the Java virtual machine. The new Class instance represents the Sample class (you can check whether it is the same instance through the hash code).

For more articles related to analyzing the loading and unloading mechanism of Java classes from the JVM, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn