Detailed graphic explanation of the process of JVM loading a class
This article mainly introduces the process of JVM loading a class. It has a very good reference value. Let’s take a look at it with the editor below
The class loading process
Java source code is compiled into class bytecode, and the JVM loads the bytecode .Class file that describes the class data. into the memory, and performs verification, conversion, analysis and initialization on the data, and finally forms a Java type that can be directly used by the virtual machine. This is the class loading mechanism of the virtual machine.
From the time a class is loaded into the virtual machine memory to the time it is unloaded from the memory, its life cycle includes: Loading, Verification, Preparation, Resolution, and Initialization. , using (Using) and unloading (Unloading) seven stages, of which the three parts of verification, preparation, and parsing are collectively referred to as links.
The order of the five stages of loading (loading), verification, preparation, initialization and unloading is fixed. The loading process of the class must start in this order, while the parsing stage does not necessarily; it can in some cases Start after initialization, this is for the runtime dynamic binding feature (also called dynamic binding or late binding, such as overriding).
1. Loading:
During the loading phase, the virtual machine mainly completes three things:
1. Obtain the binary byte stream that defines this class through the fully qualified name of a class.
2. Convert the static storage structure represented by this byte stream into the runtime data structure of the method area.
3. Generate a java.lang.Class object representing this class in the Java heap as an access entrance to the method area data
Compared to other stages of the class loading process, the loading stage (preparatory ground) Said to be the action of obtaining the binary byte stream of the class in the loading phase) is the most controllable phase in the development period, because the loading phase can be completed using the class loader (ClassLoader) provided by the system, or it can be customized by the user. The class loader is completed, and developers can control how the byte stream is obtained by defining their own class loader.
After the loading phase is completed, the binary byte stream outside the virtual machine is stored in the method area according to the format required by the virtual machine. The data storage format in the method area is defined by the virtual machine implementation, and the virtual machine does not stipulate this. The specific data structure of the region. Then an object of the java.lang.Class class is instantiated in the java heap. This object serves as the external interface for the program to access these types of data in the method area.
2. Verification:
The purpose of the verification phase is to ensure that the information contained in the byte stream of the Class file complies with the JVM specifications and will not cause harm to the JVM. If verification fails, a java.lang.VerifyError exception or its subclass exception will be thrown. The verification process is divided into four stages
1. File format verification: Verify whether the byte stream file complies with the Class file format specifications and can be processed correctly by the current virtual machine.
2. Metadata verification: It is a semantic analysis of the information described by the bytecode to ensure that the information described conforms to the specifications of the Java language.
3. Bytecode verification: It mainly analyzes the data flow and control flow to ensure that the methods of the verified class will not harm the virtual machine when running.
4. Symbol reference verification: Symbol reference verification occurs when the virtual machine converts a symbol reference into a direct reference. This conversion action will occur during the parsing phase.
3. Preparation:
The preparation phase allocates memory for variables and sets the initialization of class variables. At this stage, only class variables (static modified variables) are allocated, not class instance variables. For non-final variables, the JVM will set it to "zero value" instead of the value of its assignment statement:
pirvate static int size = 12;
Then at this stage, the value of size is 0, not 12. Class variables modified by final will be assigned real values.
4. Parsing:
The parsing phase is the process of replacing symbol references in the virtual machine constant pool with direct references.
Symbolic reference: A symbolic reference is a set of symbols to describe the referenced target object. The symbol can be any form of literal, as long as the target can be located unambiguously when used. Symbolic references have nothing to do with the memory layout implemented by the virtual machine, and the referenced target object does not necessarily have to be loaded into memory.
Direct reference: A direct reference can be a pointer directly pointing to the target object, a relative offset, or a handle that can indirectly locate the target. Direct references are related to the implementation of virtual machine memory layout. The direct references translated from the same symbol reference on different virtual machine instances are generally not the same. If there is a direct reference, the reference target must already exist in the memory.
The virtual machine specification does not stipulate the specific time when the parsing phase occurs. It only requires 13 operating symbol references during the execution of anewarry, checkcast, getfield, instanceof, invokeinterface, invokespecial, invokestatic, invokevirtual, multianewarray, new, putfield and putstatic. Before the bytecode instructions, the symbol references they use are parsed first, so the virtual machine implementation will judge as needed whether to parse the symbol references in the constant pool when the class is loaded by the loader, or wait until a A symbolic reference is resolved just before it is used.
The parsing action is mainly performed on four types of symbol references: classes or interfaces, fields, class methods, and interface methods. Corresponds to the four constant types CONSTANT_Class_Info, CONSTANT_Fieldref_Info, CONSTANT_Methodef_Info, and CONSTANT_InterfaceMethoder_Info in the compiled constant pool respectively.
1. Class and interface analysis
2. Field analysis
3. Class method analysis
4.Interface method analysis
5. Initialization:
The initialization phase of a class is the last step in the class loading process. In the preparation phase, the class variables have been assigned the initial value required by the system. In the initialization phase, it is based on the subjective plan made by the programmer through the program. Initialize class variables and other resources, or it can be expressed from another perspective: the initialization phase is to execute the class constructor
6. Use:
new thread---program counter----jvm stack execution (object reference )-----Heap memory (direct reference)----Method area
7. Uninstall:
GC garbage collection
The above is the detailed content of Detailed graphic explanation of the process of JVM loading a class. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics


![Error loading plugin in Illustrator [Fixed]](https://img.php.cn/upload/article/000/465/014/170831522770626.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

Subtitles not working on Stremio on your Windows PC? Some Stremio users reported that subtitles were not displayed in the videos. Many users reported encountering an error message that said "Error loading subtitles." Here is the full error message that appears with this error: An error occurred while loading subtitles Failed to load subtitles: This could be a problem with the plugin you are using or your network. As the error message says, it could be your internet connection that is causing the error. So please check your network connection and make sure your internet is working properly. Apart from this, there could be other reasons behind this error, including conflicting subtitles add-on, unsupported subtitles for specific video content, and outdated Stremio app. like

If you encounter freezing issues when inserting hyperlinks into Outlook, it may be due to unstable network connections, old Outlook versions, interference from antivirus software, or add-in conflicts. These factors may cause Outlook to fail to handle hyperlink operations properly. Fix Outlook freezes when inserting hyperlinks Use the following fixes to fix Outlook freezes when inserting hyperlinks: Check installed add-ins Update Outlook Temporarily disable your antivirus software and then try creating a new user profile Fix Office apps Program Uninstall and reinstall Office Let’s get started. 1] Check the installed add-ins. It may be that an add-in installed in Outlook is causing the problem.

The solutions to the problem that CSS cannot be loaded include checking the file path, checking the file content, clearing the browser cache, checking the server settings, using developer tools and checking the network connection. Detailed introduction: 1. Check the file path. First, please make sure the path of the CSS file is correct. If the CSS file is located in a different part or subdirectory of the website, you need to provide the correct path. If the CSS file is located in the root directory, the path should be direct. ; 2. Check the file content. If the path is correct, the problem may lie in the CSS file itself. Open the CSS file to check, etc.

JVM command line parameters allow you to adjust JVM behavior at a fine-grained level. The common parameters include: Set the Java heap size (-Xms, -Xmx) Set the new generation size (-Xmn) Enable the parallel garbage collector (-XX:+UseParallelGC) Reduce the memory usage of the Survivor area (-XX:-ReduceSurvivorSetInMemory) Eliminate redundancy Eliminate garbage collection (-XX:-EliminateRedundantGCs) Print garbage collection information (-XX:+PrintGC) Use the G1 garbage collector (-XX:-UseG1GC) Set the maximum garbage collection pause time (-XX:MaxGCPau

How does JavaScript achieve the infinite scroll effect of automatically loading when scrolling to the bottom of the page? The infinite scroll effect is one of the common features in modern web development. It can automatically load more content when scrolling to the bottom of the page, allowing users to obtain more data or resources without manually clicking buttons or links. In this article, we'll explore how to use JavaScript to achieve this functionality and provide specific code examples. To achieve the infinite scrolling effect of automatically loading when scrolling to the bottom of the page, it is mainly divided into the following

How does JavaScript implement the function of automatically loading more content when scrolling to the bottom of a web page? Overview: Infinite scrolling is a common feature in modern internet applications. When users scroll to the bottom of the web page, more content is automatically loaded, providing a better user experience. JavaScript can help us achieve this functionality. This article will introduce specific code examples of how to use JavaScript to listen to user scroll events and load more content based on the scroll position. Specific implementation: First, in HTM

Key points and precautions for mastering JVM memory usage JVM (JavaVirtualMachine) is the environment in which Java applications run, and the most important one is the memory management of the JVM. Properly managing JVM memory can not only improve application performance, but also avoid problems such as memory leaks and memory overflows. This article will introduce the key points and considerations of JVM memory usage and provide some specific code examples. JVM memory partitions JVM memory is mainly divided into the following areas: Heap (He
