Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the principle and usage of JVM Class loading Mechanism

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to analyze the principle and usage of JVM class loading mechanism. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. JVM class loading mechanism

The JVM class loading mechanism is divided into five parts: loading, verification, preparation, parsing, and initialization. Let's take a look at these five processes respectively.

1. Load:

Loading is the first stage in the class loading process, which generates a java.lang.Class object that represents the class in memory as an entry for all kinds of data for the class in the method area. Note that it doesn't have to be obtained from a Class file here, which is both

It can be read from the ZIP package (such as from the jar package and the war package), calculated and generated at run time (dynamic proxy), or generated by other files (such as converting the JSP file to the corresponding Class class).

two。 Verify:

The main purpose of this phase is to ensure that the information contained in the byte stream of the Class file meets the requirements of the current virtual machine and does not endanger the security of the virtual machine itself.

3. Prepare:

The preparation phase is the stage of formally allocating memory for class variables and setting the initial values of class variables, that is, the memory space used by these variables in the method area. Note the initial value concept mentioned here, for example, a class variable is defined as:

Public static int v = 8080

In fact, the initial value of the variable v after the preparation phase is 0 instead of 8080, and the put static instruction that assigns v to 8080 is compiled and stored in the class constructor method.

But note that if it is declared as:

Public static final int v = 8080

The ConstantValue attribute is generated for v in the compilation phase, and the virtual machine assigns v to 8080 based on the ConstantValue attribute in the prepare phase.

4. Parsing:

The parsing phase refers to the process in which the virtual machine replaces the symbolic reference in the constant pool with a direct reference. Symbol references are in the class file:

1. CONSTANT_Class_info

2. CONSTANT_Field_info

3. CONSTANT_Method_info

And other types of constants.

Symbolic references: symbolic references are independent of the layout implemented by the virtual machine, and the target of the reference does not have to be loaded into memory. The memory layouts of various virtual machine implementations can vary, but the symbolic references they can accept must be consistent, because the literal form of symbolic references is clearly defined in the Class file format of the Java virtual machine specification.

Direct reference: a direct reference can be a pointer to a target, a relative offset, or a handle that can indirectly locate the target. If there is a direct reference, the target of the reference must already exist in memory.

5. Initialize:

The initialization phase is the last stage of class loading, and after the previous class loading phase, all operations are dominated by JVM, except that you can customize the class loader during the loading phase. It is not until the initial stage that the actual execution of the Java program code defined in the class begins.

The initialization phase is the process of executing class constructor methods. The method is formed by the compiler automatically collecting the assignment of class variables in the class and the statements in the static statement block. Before the virtual opportunity ensures that the submethod is executed, the method of the parent class has been executed, and if there is no static variable assignment or static statement block in a class, the compiler can not generate a () method for that class.

Note that class initialization is not performed in the following situations:

1. Referencing the static field of the parent class through the subclass triggers only the initialization of the parent class, not the initialization of the subclass.

two。 Defines an array of objects that does not trigger initialization of the class.

3. Constants are stored in the constant pool of the calling class during compilation. In essence, there is no direct reference to the class that defines the constant and does not trigger the class in which the constant is defined.

4. Getting the Class object through the class name does not trigger class initialization.

5. When loading a specified class through Class.forName, the class initialization will not be triggered if the specified parameter initialize is false. In fact, this parameter tells the virtual machine whether to initialize the class.

6. The initialization action is also not triggered through the default loadClass method of ClassLoader.

Class loader

1. Start the classloader (Bootstrap ClassLoader)

Responsible for loading classes in the JAVA_HOME\ lib directory, or in the path specified by the-Xbootclasspath parameter, and recognized by the virtual machine (identified by file name, such as rt.jar).

two。 Extended class loader (Extension ClassLoader)

Responsible for loading class libraries in the JAVA_HOME\ lib\ ext directory or in the path specified by the java.ext.dirs system variable.

3. Application class loader (Application ClassLoader):

Responsible for loading the class library on the user path (classpath). JVM loads classes through the parent delegation model, but we can also implement custom class loaders by inheriting java.lang.ClassLoader.

III. Parents' appointment

When a class receives a class load request, it first does not try to load the class itself, but delegates the request to the parent class to complete, as is the case with every hierarchical class loader, so all load requests should be passed to the startup class to load, only if the parent class loader reports that it cannot complete the request (the required Class is not found under its load path) The subclass loader will try to load it itself.

One benefit of using parental delegation is, for example, to load the class java.lang.Object located in the rt.jar package, no matter which loader loads the class, it is ultimately delegated to the top-level startup class loader, which ensures that using different classloaders will end up with the same Object object.

On how to analyze the principle and usage of JVM class loading mechanism is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report