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

Example Analysis of JVM Class loading Mechanism

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly shows you the "sample analysis of JVM class loading mechanism", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of JVM class loading mechanism" this article.

A brief introduction to class loading

The loading mechanism of the class is to read the binary data of the compiled .class file into memory and create a java.lang.Class object for it to encapsulate the data structure of the class in the metadata space.

The life cycle of a class in JVM is: load, connect, initialize, use, and unload. However, only the three processes of loading, connecting and initializing are described here.

II. Loading process

Look at the detailed flow of the class loading subsystem based on a diagram:

1. Loading phase

Process description

The loading phase requires the following three processes:

Get the binary byte stream defined by the fully qualified name of the class

Transform the static storage structure represented by the byte stream into the runtime data structure of the cloud data space

Generate a java.lang.Class object representing this class in the heap Heap as an access entry to the data in the metadata space

Class loader

Boot class loader

Bootstrap-ClassLoader is based on the implementation of Java Cure + and is responsible for loading Java's core class library JAVA_HOME\ jre\ lib\ rt.jar. The loader does not inherit from the ClassLoader abstract class, and only loads the initial classes with package names such as java, javax, sun, etc., ensuring the protection of the core source code at once.

Extended class loader

Extension-ClassLoader, based on the Java language and implemented by sun.misc.Launcher$ExtClassLoader, is derived from ClassLoader abstract classes and loaded from the load class library in the path specified by the java.ext.dirs system variable, or from the jre\ lib\ ext directory of the JDK installation directory.

System class loader

Application-ClassLoader, based on the Java language and implemented by sun.misc.Launcher$ExtClassLoader, is responsible for loading the class library specified by the environment variable ClassPath. If there is no custom class loader in the application, it is generally used as the default class loader in the program.

2. Connection stage

Verification

The purpose is to ensure that the information contained in the byte stream of the Class file meets the requirements of the current virtual machine, to ensure the correctness of the loading class, and not to endanger the security of the virtual machine itself, including four kinds of verification actions:

File format validation: verifies that the byte stream conforms to the specification of the Class file format

Metadata validation: ensure that the information it describes conforms to the requirements of the Java language specification

Bytecode verification: make sure that program semantics are logical

Symbol reference validation: ensure that the parsing action is performed correctly.

Prepare for

Memory is allocated to static variables of a class and initialized to default values. At this time, memory allocation only includes class variable (static) modification, not (final-static) modification. Here, instance variables are not allocated and initialized, and instance variables are allocated to the Java heap along with the object.

Analysis

The process of converting a symbolic reference in a constant pool to a direct reference, which is a pointer directly to the target, a relative offset, or a handle that indirectly locates to the target. Parsing is mainly aimed at classes or interfaces, fields, class methods, interface methods, method types, and so on. The parsing action is actually performed with JVM after initialization.

3. Initialization phase

The process of executing the class constructor clinit () method, which does not need to be customized, is the combination of the assignment action of all class variables in the class automatically collected by the javac compiler and the statements in the static code block. Jvm wants to ensure the security of the clinit () method under multithreaded access.

Third, mechanism strategy 1, parental appointment model

When a class loader receives a request for a class load, it does not attempt to load the class first, but delegates the request to the parent loader to execute

If the parent loader still has a parent class loader, it is delegated upward in turn, so the class load request should eventually be passed to the top-level startup class loader

If the parent class loader can complete the class load request, it returns successfully. Only when the parent loader is unable to complete the load, the child loader will try to load the class itself.

2. Sandbox security mechanism

Suppose a custom class is named String and the package is java.lang. When loading with bootstrap class loader, the String class in JDK will be loaded first, because this class originally belongs to jdk, and the String class will report an error when it appears again, so as to ensure that the source code will not be maliciously tampered with. This is the sandbox security mechanism.

The above is all the content of the article "sample Analysis of JVM Class loading Mechanism". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Internet Technology

Wechat

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

12
Report