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

What is the class loading mechanism of JVM

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

Share

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

This article mainly introduces "what is the class loading mechanism of JVM". In the daily operation, I believe that many people have doubts about what is the class loading mechanism of JVM. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the class loading mechanism of JVM?" Next, please follow the editor to study!

The life cycle of a class in JVM consists of seven phases, load, prepare, validate, parse, initialize, use, and unload. Among them, preparation, verification and parsing are classified as the connection phase.

Load

What jvm has done at this stage

Get the binary byte stream of the class through the class name

Convert the static storage structure represented by this byte stream into the runtime data structure of the method area

Generate a java.lang.class object representing the class in the heap as an entry to access the class's data in the method area

At this stage, developers can control the acquisition of binary byte streams, that is, they can do their own customized operations through custom class loaders.

Verification

As the name implies, verify the correctness of the loaded class.

File format verification: verify that the byte stream conforms to the specification of Class file format; for example, whether it starts with 0xCAFEBABE

Metadata validation: semantic analysis of the information described by bytecode to ensure that the information described meets the requirements of the Java language specification

Bytecode verification: through the analysis of data flow and control flow, it is determined that the program semantics are legal and logical.

Prepare for

Allocate memory for static variables of the class and initialize them to default values

Only static variable memory is allocated

The initialization default is the default value of the type (that is, int:0, boolean:false...), not the initial value of the code display setting

If it is a final static-decorated variable, it is assigned to the initial value in the code (that is, final static int val=3, where the val assignment is 3 instead of 0).

Analysis

Converting a symbolic reference in a class to a direct reference is a set of symbols that describe the target (for example: ArrayList). A direct reference is a pointer directly to the target, a relative offset, or a handle that indirectly locates to the target.

Initialization

Initialization of class variables

Class when a class variable is defined

Static code block initialization

The scenario that triggers class initialization

Create a class instance, that is, a new object

Access static variables

Access static methods

Reflection call (that is, Class.forName ("com.xxx.Obj"), the Obj class is initialized)

If the subclass is initialized, the parent class is initialized.

Class loader

Boot class loader (BootStrap ClassLoader): responsible for loading class libraries that can be recognized by jvm under jrelib or under the path specified by the-Xbootclasspath parameter. Developers cannot use it directly

Extended class loader (Extension ClassLoader): sun.misc.Launcher$ExtClassLoader, which is responsible for loading all class libraries in the jrelibext directory or in the path specified by the java.ext.dirs system variable. Developers can use it directly.

Application class loader (Application ClassLoader): sun.misc.Launcher$AppClassLoader, which is responsible for loading the classes specified by the user classpath (ClassPath). Developers can directly use the

Custom class loader (Custom ClassLoader): you can customize the class loader

Parental delegation model

image.png

In the implementation of the parent delegation model, when a class loader needs to load a class, the task will be delegated to the parent class loader, up and down until the top level starts the class loader, and if the parent cannot load, it will handle the loading itself. The advantage of the parent delegation model is to ensure that there is only one same class in the same environment. That is to say, the condition in JVM to determine whether it is the same class is whether the same class loader is the same class itself. Code example:

Public class ClassLoaderTest {public static void main (String [] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {/ / use ClassLoaderTest's classloader to load this class Object obj1 = ClassLoaderTest.class.getClassLoader (). LoadClass ("com.ognice.ClassLoaderTest"). NewInstance (); System.out.println (obj1.getClass (). GetClassLoader ()); System.out.println (obj1 instanceof ClassLoaderTest) / / use custom class loader to load this class ClassLoader customClassLoader = new ClassLoader () {@ Override public Class loadClass (String name) throws ClassNotFoundException {System.out.println ("custom classloader loading" + name); String fileName = name.substring (name.lastIndexOf (".") + 1) + ".class"; InputStream stream = getClass () .class (fileName) If (stream = = null) {return super.loadClass (name);} try {byte [] b = new byte [stream.available ()]; stream.read (b); return defineClass (name, b, 0, b.length) } catch (IOException e) {e.printStackTrace ();} / / parent looking for class return super.loadClass (name);}}; Object obj2 = customClassLoader.loadClass ("com.ognice.ClassLoaderTest"). NewInstance (); System.out.println (obj2.getClass (). GetClassLoader ()) System.out.println (obj2 instanceof ClassLoaderTest);}}

Execution result

Sun.misc.Launcher$AppClassLoader@18b4aac2truecustom classloader loading com.ognice.ClassLoaderTestcustom classloader loading java.lang.Objectcustom classloader loading java.lang.ClassLoadercustom classloader loading com.ognice.ClassLoaderTest$1com.ognice.ClassLoaderTest$1@277c0f21false at this point, the study on "what is the class loading mechanism of JVM" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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