This article is participating in “Java Theme Month – Java Swipe Card”, see the activity link for details

Title: Describe the JVM class loading mechanism?

Knowledge:

The entire life cycle of a class from being loaded into the memory of the virtual machine to being unloaded from the memory includes: The Loading, Linking, initialization, Using, and Unloading phases, The link stage can be divided into Verification, Preparation and Resolution.

The sequence flow of the whole stage is shown in the figure below:

The order of load, validation, preparation, initialization, and unload is determined, and the class loading process is loaded along this general flow, but the parsing phase is not necessarily fixed, and in some cases it starts after the initialization phase, in order to support runtime binding (dynamic binding) in the Java language.

The five steps of loading, validation, preparation, parsing, and initialization constitute a complete class loading process.

loading

There are two moments when class loading is triggered:

  1. This jar package is often used to run programs such as java.io, java.lang, and java.util. These packages are loaded with the virtual machine. To view the class loading information, write an empty main function and set the VM parameter to [-xx :+TraceClassLoading].

  2. Runtime loading. When a vm uses a.class file, it checks memory to see if the.class file has been loaded. If not, it loads the class with its fully qualified name.

In the loading phase, we mainly do the following three steps:

  • Gets the binary byte stream that defines a class through its fully qualified name
  • Convert the static storage structure represented by the.class file into the runtime data structure of the method area
  • A java.lang. Class object of this Class is generated in the Java heap as an entry point to the data in the method area

validation

Link phase 1 – To ensure that the byte stream of the.class file contains information that meets the requirements of the current VM and does not compromise vm security.

In the validation phase, the following steps are performed:

  • File format validation

The 5th to 8th bytes of the bytecode file indicate the major version number of the bytecode file, and the validation will be performed on these four bytes. Later versions of the JDK are backward compatible with the earlier version of the. Class file, but cannot run the later version of the CLASS file. Throws the Java. Lang. UnsupportedClassVersionError.

  • Metadata validation
  • Bytecode verification
  • Symbolic reference validation

To prepare

The preparation phase is the process of allocating memory for class static variables and setting their initial values. The memory used by these variables will be allocated in the method area.

Note:

  • This allocates memory only for class variables (variables that are static modified), not instance variables, which are allocated in the Java heap along with the object when it is instantiated
  • This phase assigns initial values to static variables that are not final. For example: [public static int value = 11]; [public static int value = 11]; For example, [public static final int value = 11] is different. In the preparation phase, the virtual machine will assign value to 11.

parsing

The parse phase is the process in which the VIRTUAL machine replaces the symbol quotes in the constant pool with direct quotes. First look at the difference between symbolic quotes and direct quotes:

Symbol quotes

  • Fully qualified names of classes and interfaces
  • The name and descriptor of the field
  • The name and descriptor of the method

Symbolic quotes are descriptions of classes, variables, and methods. Symbolic references are independent of the memory layout of the virtual machine, and the target of the reference is not necessarily loaded into memory.

Direct reference

A direct reference is a pointer directly to the target, the relative offset; Direct quotes are related to the memory layout implemented by the virtual machine. The same direct quotes are generally not the same in different virtual machines. If there is a direct reference, the referenced object must already exist in the Java heap memory.

Initialize the

The initialization phase is the last step in the class loading process. Initialization involves assigning user-specified values to static variables and executing static code blocks.

Note:

The virtual machine ensures that class initialization is properly locked and synchronized in multithreaded scenarios. If multiple threads are initializing the same class at the same time, then only one thread will execute the class’s clinit() method, and the other threads will block and wait until the thread that acquired the lock completes clinit(). If there is a lengthy operation in a class’s clinit() method, The clinit() method will not be used again after the thread executing clinit() completes, because a class is initialized only once under the same class loader.

The Java Virtual Machine specifies that classes must be initialized immediately for the following scenarios, which are called active references to a class.

  • Use the new keyword to instantiate an object, read or set a static field of a class, and call a static method of a class
  • Make a reflection call to the class using the methods in the java.lang.Reflect package
  • When a class is initialized, its parent class is not initialized
  • The virtual machine starts by initializing the user-specified class that contains the main() method