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 principle and mechanism of JVM loading class files?

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

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge about the principle and mechanism of loading class files in JVM. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

A brief introduction to JVM

JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine). JVM is a specification for computing devices. It is a fictional computer, which is realized by simulating various computer functions on a real computer.

A very important feature of Java language is its platform independence. The use of Java virtual machine is the key to realize this feature. If a general high-level language is to run on different platforms, it needs to at least be compiled into different object code. After introducing the Java language virtual machine, the Java language does not need to be recompiled when it runs on different platforms. The Java language uses the Java virtual machine to shield the information related to the platform, so that the Java language compiler only needs to generate the object code (bytecode) running on the Java virtual machine and can run on a variety of platforms without modification. When the Java virtual machine executes the bytecode, it interprets the bytecode as the execution of its instructions on the specific platform. This is why Java can "compile once, run everywhere".

II. Components of JVM

As can be seen from the figure, JVM runs on the operating system and has no direct interaction with the hardware.

1. Classloader Class Loader

The role of the class loader is to load the class file into memory, such as writing a HelloWorld.java program, and then compiling the class file through javac. The class file is loaded into memory by Class Loader. However, there are format requirements for Class Loader to load class files.

Note: Class Loader is only loaded, as long as it meets the file structure, and Execution Engine is responsible for whether it can be run or not.

two。 Execution engine Exexution Engine

The execution engine, also known as the interpreter, is responsible for interpreting commands and submitting the operating system for execution.

3. Local interface Native Interface

The purpose of the local interface is to integrate different programming languages for Java. Its original intention is to integrate the C native + program, when Java was born, when it ran rampant, to gain a foothold, there must be a smart and wise call to the Cmax Cure + program, so it opened up a special area in memory to deal with the code marked as native. Its specific approach is to register the native method in Native Method Stack and load native libraries when Execution Engine is executed. At present, this method can only be used in hardware-related applications, and it has been relatively rare in enterprise applications, because the communication between heterogeneous fields is very developed, such as Socket communication, WebService and so on.

4. Run datazone Runtime data area

Running the datazone makes the whole JVM focus. All the programs we write are loaded here before they start to run, and the Java ecosystem is so prosperous, thanks to the excellent autonomy of the region.

Third, the principle and mechanism of loading class files by JVM.

1. All the classes in Java must be loaded into JVM to run. This loading work is done by the class loader in JVM. The essence of the class loader is to read the class files from the hard disk to memory, and the function is to load the class at run time.

The Java classloader is based on three mechanisms: delegation, visibility, and oneness.

(1) delegation mechanism refers to the request to load a class to the parent class loader, if the parent class loader can not find or load the class, then load it.

(2) the principle of visibility is that the subclass loader can see all the classes loaded by the parent class loader, while the parent class loader cannot see the classes loaded by the subclass loader.

(3) the singleness principle means that a class is loaded only once, which is a delegate mechanism to ensure that the subclass loader will not load the class loaded by the parent class loader again.

2. There are three types of classes in Java:

(1) system class

(2) extended class

(3) classes defined by programmers

3. There are two ways to load a class

(1) implicit loading:

When the program encounters the static domain of generating class or subclass object or using class or subclass through new, the class loader is implicitly called to load the corresponding class into JVM.

(2) explicit loading:

Explicitly load the desired class by calling methods such as Class.forName () or ClassLoader.loadClass (className).

4. The dynamic embodiment of class loading

An application is always made up of n classes. When a Java program starts, it does not load and run all the classes at once. It always loads the basic classes that ensure that the program runs into JVM at once, and other classes wait until JVM is used to load them. This is to save memory overhead, because Java was originally designed for embedded systems, and memory is precious. And loading when used is also a manifestation of the dynamic nature of Java.

5. Java class loader

The class loader in Java is also a class in essence, and the function is to load the class into JVM. It is worth noting that there are three class loaders in JVM: on the one hand, in order to clarify the division of labor, each is responsible for their own blocks, on the other hand, in order to achieve the delegation model.

The hierarchy is as follows:

BootStrap Loader (bootstrap class loader)-responsible for loading system classes

ExtClassLoader (extension class loader)-responsible for loading extension classes

AppClassLoade (application class loader) r-responsible for loading application classes

6. How to coordinate the work between class loaders

There are three class loaders in Java. When a class needs to be loaded, Java uses the delegate model mechanism to coordinate and distinguish which class loader should complete. To put it simply, "when the classloader has the need to load the class, it will first ask its Parent to use its search path to help load". If the Parent can not find it, it is up to it to search the class according to its own search path.

Example 1:

Package ClassLoaderTest;public class ClassLoaderTest {public static void main (String [] args) {ClassLoader C1 = ClassLoaderTest.class.getClassLoader (); System.out.println (C1); ClassLoader c1Parent = c1.getParent (); System.out.println (c1Parent); ClassLoader c1Root = c1Parent.getParent (); System.out.println (c1Root);}}

Execution result:

You can see that the ClassLoaderTest is loaded by the AppClassLoader loader. AppClassLoader's Parent loader is ExtClassLoader. But ExtClassLoader's Parent is null and is not available in Java.

Example 2:

Public class Test2 {public void test () {System.out.println (Test2.class); System.out.println (this.getClass ()); System.out.println (Test2.class.getClassLoader ());}} public class Test1 {public static void main (String [] args) {System.out.println (Test1.class.getClassLoader ()); Test2 test2 = new Test2 (); test2.test ();}}

Execution result:

7. Pre-loaded and loaded on demand

In order to optimize the system and improve the execution speed of the program, the Java running environment will load all the basic classes needed for Java running into memory by preloading (pre-loading) at the beginning of JRE running, because these units are often used in the process of running Java programs, including all .class files in JRE's rt.jar files.

When the java.exe virtual machine starts running, it will find the JRE environment installed on the machine, and then the class loader that gives control to JRE,JRE will automatically load the rt.jar basic category file library under the lib directory into memory. These files are necessary for Java program execution, so the system loads these files at the beginning to avoid multiple IO operations in the future, thus improving the efficiency of program execution. However, when we need to use custom classes in the program, we have to use the method of loading on demand (load-on-demand), that is, loading when the Java program needs it, in order to reduce memory consumption.

8. Some important methods in ClassLoader

9. Where is the class loader applicable?

The most classic example is AppletClassLoader, which is used to load classes used by Applet, while Applet is mostly used online rather than by local operating systems. Using different class loaders, you can load the same class from different source addresses, and they are treated as different classes. J2EE uses multiple classloaders to load classes in different places, for example, the War file is loaded by the Web-app classloader, while the classes in EJB-JAR are loaded by another classloader. Some servers also support hot deployment, which is implemented by classloaders. You can also use class loaders to load data from databases or other persistence layers.

10. Hierarchical system of class loader

How the Java class loader works:

When executing the .class file of Java, java.exe will help us find jRE, then find the jcm.dll inside JRE, which is the real Java virtual machine, and finally load the dynamic library and activate the Java virtual machine. After the virtual machine is activated, some initialization actions will be done first, such as reading system parameters. Once the initialization action is completed, the first classloader-BootstrapLoader, BootstrapLoader is written by C++. In the initial work done by this Bootstrap, in addition to some basic initialization actions, the most important thing is to load the ExtClassLoader in the Launcher.java and set its parent to null, representing its parent loader as BootstrapLoader. Bootstrap loader then requires that the AppClassLoader in the Launcher.java be loaded and its parent set to the previously generated ExtClassLoader entity. Both class loaders exist in the form of static classes. Note: Launcher E x t C l a s s L o a d er. Class and Launcher ExtClassLoader.class as well as Launcher ExtClassLoader.class and LauncherAppClassLoader.class are loaded by Bootstrap Loader, so it doesn't matter which classloader loads the Parent.

The relationship between the three:

Bootstrap Loader

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