Java Virtual Machine Learning - Class Loading Mechanism
Class loading mechanism
The loading mechanism is the process in which the JVM loads the class file into the memory, verifies, converts, parses and initializes the data, and finally forms a Java type that can be directly used by the JVM.
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, There are seven stages: Initialization, Using, and Unloading, among 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, and the parsing stage Not necessarily; it can in some cases start after initialization, for the runtime dynamic binding feature. It is worth noting that these stages are usually intertwined and mixed, usually calling or activating another stage during the execution of one stage.
Loading:
The loading stage is a stage in the "class loading mechanism". This stage is usually also called "loading" and mainly completes:
1. Get the binary byte stream that defines this class through the "full name of the class"
2. Convert the static storage structure represented by the byte stream into runtime data in the method area Structure
3. Generate a java.lang.Class object representing this class in the java heap as the access entry for these data in the method area
Virtual machine specifications for "through" the full name of the class "To obtain the binary byte stream that defines this class" does not specify that the binary stream must be obtained from a local class file. To be precise, it does not specify where and how to obtain it. For example:
Reading from Zip packages is very common and eventually becomes the basis for future JAR, EAR, and WAR formats.
Obtained from the Internet, common application Applet.
Run-time calculation and generation. The most commonly used in this scenario is dynamic proxy technology. In java.lang.reflect.Proxy, ProxyGenerator.generateProxyClass is used to generate the binary of $Prxoy's proxy class for a specific interface. byte stream.
is generated from other format files. Typical scenario: JSP application
reads from the database. This scenario is relatively rare. Some middleware servers (such as SAP Netweaver) can choose to install the program to Database to complete the distribution of program code among clusters.
Compared with other stages of the class loading process, the loading stage (preparatory speaking, the action of obtaining the binary byte stream of the class in the loading stage) is the most controllable stage during the development period, because the loading stage This can be done using the class loader (ClassLoader) provided by the system, or it can be done by a user-defined class loader. Developers can control the acquisition method of the byte stream 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. The specific data structure of this area is not specified. 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. Parts of the loading phase and the linking phase (such as part of the bytecode file format verification actions) are interleaved. The loading phase has not yet been completed, and the linking phase may have started, but these actions sandwiched between the loading phase still belong to the link. The contents of the stages and the start times of these two stages still maintain a fixed sequence.
Carry out data flow and control flow analysis. At this stage, the method body of the class is verified and analyzed. The task of this stage is to ensure that the method of the class being verified will not perform behavior that endangers the security of the virtual machine during runtime. For example: ensure that the type conversion in the access method body is valid. For example, you can assign a subclass object to a parent class data type. This is safe, but you cannot assign a parent class object to a subclass data type. Ensure that the jump command does not Will jump to bytecode commands outside the method body.
4. Symbol reference verification
Whether the fully qualified name described by the string in the symbol reference can find the corresponding class, the accessibility of the classes, fields and methods in the symbol reference class (private , protected, public, default) can be accessed by the current class.
Preparation:
The preparation stage is the stage where memory is officially allocated for class variables and the initial values of class variables are set. These memories will be performed in the method area. distribute. There are two knowledge points that are easily confusing at this stage. First, memory allocation at this time only includes class variables (static modified variables), not instance variables. Instance variables will follow the object when the object is instantiated. allocated together in the java heap. Secondly, the initial value mentioned here is "normally" the zero value of the data type. Suppose a class variable is defined as:
public static int value = 12;
Then the variable value is The initial value after the preparation phase is 0 instead of 12, because no Java method has been executed yet, and the putstatic instruction that assigns value to 123 is stored in the class constructor
The initial value is zero under the "normal circumstances" mentioned above. Compared with some special cases, if there is a ConstantValue attribute in the field attribute table of the class field, then the variable value will be changed during the preparation phase. Initialized to the value specified by the ConstantValue attribute, the above class variable value is defined as:
public static final int value = 123;
When compiling, javac will generate the ConstantValue attribute for value. In preparation The stage virtual machine will set the value to 123 according to the ConstantValue setting.
Parsing:
The parsing phase is the process of replacing symbol references in the virtual machine constant pool with direct references.
Symbol reference: A symbol 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 the execution of 13 steps: anewarry, checkcast, getfield, instanceof, invokeinterface, invokespecial, invokestatic, invokevirtual, multianewarray, new, putfield and putstatic. Before the bytecode instructions used to operate symbol references, the symbol references they use are parsed first, so the virtual machine implementation will judge based on needs whether the symbol references in the constant pool are processed when the class is loaded by the loader. Parse, or wait until a symbol reference is about to be used before parsing it.
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
Initialization:
The initialization phase of a class is the last step of the class loading process. In the preparation phase, the class variable has been assigned the initial value required by the system, and in the initialization phase , it is to initialize class variables and other resources according to the subjective plan made by the programmer through the program, or it can be expressed from another perspective: the initialization phase is the process of executing the class constructor
1. When encountering the four bytecode instructions of new, getstatic, putstatic or invokestatic, if the class has not been initialized, it needs to be triggered first its initialization. The most common Java code scenarios that generate these four instructions are: using the new keyword to instantiate an object, reading or setting static fields of a class (except for static fields that are modified by final and have been put into the constant pool by the compiler) ), and when calling static methods of the class.
2. When using the method of the java.lang.reflect package to make a reflection call to a class
3. When initializing a class, if it is found that its parent class has not been initialized, You need to start the initialization of its parent class first
4. When jvm starts, the user specifies a main class for execution (the class containing the main method), and the virtual machine initializes this class first.
In the above preparation stage, public static int value = 12; In preparation After the stage is completed, the value of value is 0, and the class constructor
*The class constructor
*The class constructor
*Since the
*
*Static statement blocks cannot be used in interfaces, but what is not possible with interfaces and classes is that executing the
*The virtual machine ensures that the
The above is the content of Java Virtual Machine Learning - Class Loading Mechanism. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!
Related articles:
Detailed explanation of Java virtual machine
In-depth understanding of Java virtual machine
Java Virtual Machine Learning - Object Memory Allocation and Recycling

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

Essentials for Java development: Detailed explanation of Java virtual machine installation steps, specific code examples required. With the development of computer science and technology, the Java language has become one of the most widely used programming languages. It has the advantages of cross-platform and object-oriented, and has gradually become the preferred language for developers. Before using Java for development, you first need to install the Java Virtual Machine (JavaVirtualMachine, JVM). This article will explain in detail the installation steps of the Java virtual machine and provide specific code examples.

With the continuous development of the Internet, more and more applications and businesses require the use of programs developed in the Java language. For the running of Java programs, the performance of the Java Virtual Machine (JVM) is very important. Therefore, optimizing configuration is an important means to improve the performance of Java applications. Pagoda panel is a commonly used server control panel that can help users manage servers more conveniently. This article will introduce how to use the Pagoda panel to optimize the configuration of the Java virtual machine. Step one: Install Java virtual machine

The Java virtual machine uses reference counting to manage memory usage. When the reference count of an object reaches 0, the JVM will perform garbage collection. The reference counting mechanism includes: each object has a counter that stores the number of references pointing to the object. When the object is created, the reference counter is set to 1. When an object is referenced, the reference counter is incremented. When the reference ends, the reference counter is decremented.

The stack frame is the basic data structure for executing methods in the Java Virtual Machine (JVM), and includes the following parts: Local variable table: stores the local variables of the method. Operand stack: stores operands and intermediate results. Frame data: Contains return address and current program counter. The functions of the stack frame include: storing local variables. Perform operand operations. Handle method calls. Assist with exception handling. Assisted garbage collection.

Detailed explanation of JVM principles: In-depth exploration of the working principle of the Java virtual machine requires specific code examples 1. Introduction With the rapid development and widespread application of the Java programming language, the Java Virtual Machine (JavaVirtualMachine, referred to as JVM) has also become indispensable in software development. a part of. As the running environment for Java programs, JVM can provide cross-platform features, allowing Java programs to run on different operating systems. In this article, we will delve into how the JVM works

Explore: The working principle and core functions of the Java Virtual Machine Introduction: The Java Virtual Machine (JavaVirtualMachine, JVM for short) is the core part of Java program running. It is responsible for compiling Java source code into executable bytecode and executing it. This article will delve into the working principles and core functions of the Java virtual machine, and use specific code examples to help readers better understand. 1. Working Principle of Java Virtual Machine 1.1 Class Loader (ClassLoader) J

Java core technology stack: In-depth understanding of the Java language, Java virtual machine, and JavaSE library. With the continuous development of computer science and technology, the Java language has become one of the most popular programming languages in the world. As a cross-platform high-level programming language, Java is widely used in various fields, especially in enterprise-level application development and cloud computing. To become an excellent Java developer, you must be proficient in the Java core technology stack, namely Java language, Java virtual machine and Java

Java Virtual Machine Installation Guide: Teach you step by step how to install, need specific code examples Introduction: Java Virtual Machine (JavaVirtualMachine, referred to as JVM) is a virtual machine that can run Java bytecode. It is a core component of Java technology and the key to Java applications being able to run across platforms. In this article, we will teach you step by step how to install a Java virtual machine and provide specific code examples to help you get started quickly. 1. Download JavaDev
