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 implement Class loading Mechanism in Java

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

Share

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

In this issue, the editor will bring you about how to implement the class loading mechanism in Java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

To run the Java program, you need to load the compiled class file into the JVM runtime data area.

Before we understand the loading mechanism of a class, we need to understand the life cycle of the class. The entire life cycle of a Java class from being loaded into JVM memory to unloading out of memory includes seven phases: loading (Loading), Verification (Verification), preparation (Preparation), Resolution (parsing), initialization (Initialization), usage (Using) and unloading (Unloading).

Class loader

The Java class needs to be loaded with a classloader. The class loader is responsible for loading classes, searching networks, jar,zip, folders, binary data, memory, and other resources at specified locations. A Java program runs with at least three different classloader instances that are responsible for loading different classes. The three classloaders are startup classloader (Bootstrap ClassLoader), extended classloader (Extension ClassLoader), and application classloader (Application ClassLoader).

The classloader can be viewed through API:java.lang.Class.getClassLoader () provided by JDK, which returns the classloader where the class is loaded, and if the class is loaded by Bootstrap ClassLoader, this method returns null.

Code example:

Public class ClassLoaderView {

Public static void main (String [] args) throws Exception {

/ / load the BootStrap ClassLoader of the core class library

System.out.println (

"Core class library loader:"

+ ClassLoaderView.class

.getClassLoader ()

.loadClass ("java.lang.String")

.getClassLoader ()

/ / load the Extension ClassLoader of the extension library

System.out.println (

"extend the class library loader:"

+ ClassLoaderView.class

.getClassLoader ()

.loadClass ("com.sun.nio.zipfs.ZipCoder")

.getClassLoader ()

/ / load the Application ClassLoader of the application

System.out.println ("Application Library Loader:" + ClassLoaderView.class.getClassLoader ())

}

}

/ / Operation result:

Core class library loader: null

Extended class library loader: sun.misc.Launcher$ExtClassLoader@7f31245a

Application library loader: how does sun.misc.Launcher$AppClassLoader@18b4aac2JVM know where our classes are

Class information can exist in different places, so how does JVM know where our classes exist? By looking at the source code of sun.misc.Launcher.AppClassLoader, we can see that it reads the configuration of java.class.path to get those address load class resources. Refer to the following code example, which can be verified by using the jsp and jcmd commands.

Code example:

Public class HelloWord {

Public static void main (String [] args) throws IOException {

System.out.println ("Hello Word")

System.in.read ()

}

}

The jsp command can view the native Java process, and the jcmd command can view the runtime configuration: jcmd process number VM.system_properties

Parental delegation model

Classes in Java do not load repeatedly, the same class loader, the same class name, represents the same class. The main reason to avoid repeated class loading is that JVM defaults to the parent delegation model when loading classes. The so-called parent delegation model means that when a specific class loader receives a load request for a class, it first delegates the loading task to the parent loader, recursively, and returns successfully if the parent loader can complete the class loading task; only when the parent loader cannot complete the loading task, it loads it itself. Delegate from bottom to top and search from top to bottom. The parental delegation model ensures the type safety of the Java core library.

Unloading of class

Classes in JVM cannot exist all the time, and classes will be unloaded if certain conditions are met. If all instances of the Class have been garbage collected and the ClassLoader instances that loaded the class have been garbage collected, the class will be unloaded by JVM. Add the-verbose:class parameter to JVM startup to output log information about class loading and unloading.

This is how to implement the class loading mechanism in Java shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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