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?

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

Share

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

This article mainly explains "what is the class loading mechanism". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the class loading mechanism"?

Class recording process

Several java files are compiled and packaged to generate a runnable jar package, and finally the java command runs the main function startup program of a main class. First of all, the main class needs to be loaded into jvm through the class loader. If the main class uses other classes during operation, these classes will be loaded step by step. Note that the classes in the jar package are not loaded all at once, they are loaded when they are in use.

The whole process from class loading to using consists of the following steps: load, verify, prepare, parse, initialize, use, and unload.

Load: find the bytecode file on the hard disk and read it through IO. It will only be loaded when you use the class, such as calling the main method of the class, new object, etc.

Verify: verify the correctness of the bytecode file

Prepare: allocate memory to static variables of the class and assign default values

Parsing: replace symbolic references with direct references. In this stage, some static methods (symbolic references, such as main methods) are replaced with pointers or handles to the memory where the data is located (direct references). This is the static linking process, which is completed during class recording. Dynamic linking is done during the run of the program to replace symbolic references with direct references.

Initialization: initializes the static variable of the class to the specified value and executes the static code block.

Class loader

The above class loading process is mainly implemented through the class loader, and there are several kinds of loaders in java.

Startup class loader: responsible for loading the core class libraries in the JREd lib directory that support the JVM operation

Extension class loader: responsible for loading JAR class packages that support JVM to run in the ext extension directory located in the lib directory of JRE

Application loader: responsible for loading class packages under the ClassPath path, mainly loading application classes

Custom loader: responsible for loading class packages under the user-defined path

The class loader inherits the java.lang.ClassLoader class, which has two core methods, loadClass and findClass.

Protected Class loadClass (String name, boolean resolve) throws ClassNotFoundException {synchronized (getClassLoadingLock (name)) {/ / First, check if the class has already been loaded Class c = findLoadedClass (name); if (c = = null) {long t0 = System.nanoTime (); try {if (parent! = null) {c = parent.loadClass (name, false) } else {c = findBootstrapClassOrNull (name) } catch (ClassNotFoundException e) {/ / ClassNotFoundException thrown if class not found / / from the non-null parent class loader} if (c = = null) {/ / If still not found, then invoke findClass in order / / to find the class. Long T1 = System.nanoTime (); c = findClass (name); / / this is the defining class loader; record the stats sun.misc.PerfCounter.getParentDelegationTime (). AddTime (T1-T0); sun.misc.PerfCounter.getFindClassTime (). AddElapsedTimeFrom (T1); sun.misc.PerfCounter.getFindClasses (). Increment () }} if (resolve) {resolveClass (c);} return c;}}

The default implementation of the findClass method is to throw an exception, so we mainly override the findClass method in the custom class loader.

Protected Class findClass (String name) throws ClassNotFoundException {throw new ClassNotFoundException (name);} Parental delegation Mechanism

The jvm loader has a parent-child layer structure.

Here, class loading is the parent delegation mechanism. When recording a class, it will first delegate the parent loader to find the target class, and then delegate the upper parent loader to load if it cannot be found. If all the parent loaders cannot find the target class under their own loading class path, then find and load the target class in their own class loading path.

For example, the Math class will first find the application class loader to load, the application loader will first delegate the extension class loader to load, the extension class loader will then delegate to start the class loader, and the top-level startup class loader will find the Math class in its own classloader path for a long time, then it will return the request to load the Math class downwards, and the extension class loader will load itself when it receives a reply. After searching for a long time in its own class loading path, I couldn't find the Math class, and then returned the load request of the Math class to the application class loader, so the application class loader found the Math class in its own class loading path and loaded it itself.

The parent delegation mechanism is simply: find the father to load first, and then load it by the father himself.

At this point, I believe you have a deeper understanding of "what is the class loading mechanism". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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