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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment