In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "jvm class loader, what is class loading mechanism". In daily operation, I believe that many people have doubts about jvm class loader and class loading mechanism. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "jvm class loader, class loading mechanism". Next, please follow the editor to study!
Java code execution flowchart
Through this flow chart, we can understand how the Java code we wrote is executed, in which we have to go through the process of class loader, so let's talk about the knowledge points in detail.
Class loading subsystem
Class loading system architecture diagram
It doesn't matter if I can't understand these two pictures for the time being. Follow my brother and look down.
The life cycle of a class
The life cycle of a class includes: loading, linking, initializing, using and unloading, in which loading, linking, initializing, belongs to the process of class loading, which we will explain in detail below. Usage refers to the use of our new object, and unloading means that the object has been garbage collected.
The process of class loading
Step 1: Loading loading
Get the binary byte stream of the .class file of the class through the fully qualified name of the class (package name + class name)
Transform the static storage structure represented by the binary byte stream into the data structure of the method area runtime.
Generate a java.lang.Class object that represents the class in memory as the access entry for all kinds of data of the class in the method area
Summary: load binary data into memory-> map to a structure that jvm can recognize-> generate class files in memory.
Step 2: Linking links
Linking refers to the process of merging the above created class classes into the Java virtual machine so that it can be executed, which can be divided into three stages: verification, preparation, and parsing.
① authentication (Verify)
Ensure that the byte stream in the class file contains information that meets the requirements of the current virtual machine, ensures the correctness of the loaded class class, and will not endanger the security of the virtual machine.
② preparation (Prepare)
Allocate memory for static fields in the class and set default initial values, such as 0 for the int type. The static field modified by final is not set because final is assigned at compile time
③ parsing (Resolve)
The purpose of the parsing phase is to convert symbolic references within a constant pool into direct references (to resolve symbolic references within a constant pool into actual references). If a symbolic reference points to an unloaded class, or a field or method of an unloaded class, parsing will trigger the loading of the class (but not necessarily the link and initialization of the class. )
In fact, parser operations are often accompanied by JVM after initialization. A symbolic reference is a set of symbols that describe the referenced target. The literal form of symbolic references is clearly defined in the Class file format of the Java virtual machine specification. A direct reference is a pointer directly to the target, a relative offset, or a handle that indirectly locates to the target.
Parsing actions are mainly aimed at classes, interfaces, fields, class methods, interface methods, method types and so on. It corresponds to CONSTANT_Class_info, CONSTANT_Fieldref_info, CONSTANT_Methodref_info and so on in the constant pool.
Step 3: initialize initialization
Initialization is the process of executing the class's constructor method init ().
This method does not need to be defined and is merged by the javac compiler automatically collecting the assignment actions of all class variables in the class and the statements in the static block of code.
If the class has a parent class, jvm ensures that the init of the parent class is executed first, and then the init of the subclass is executed.
Classification of class loaders
First: startup class / boot class: Bootstrap ClassLoader
This class loader is implemented in CCompact + language and is nested inside JVM. The java program cannot directly manipulate this class.
It is used to load Java core class libraries, such as: JAVA_HOME/jre/lib/rt.jar, resources.jar, sun.boot.class.path under the path of the package, used to provide jvm to run the necessary packages.
Does not inherit from java.lang.ClassLoader, which does not have a parent loader
It loads the extension class loader and the application class loader and becomes their parent class loader
For security reasons, startup classes only load classes with package names beginning with: java, javax, sun.
Second: extension class loader: Extension ClassLoader
Written in Java and implemented by sun.misc.Launcher$ExtClassLoader, we can use Java programs to operate the loader
Derives from java.lang.ClassLoader, and the parent class loader is the startup class loader
Load the class libraries from the system properties: java.ext.dirs directory, or from the JDK installation directory: jre/lib/ext directory. We can put our own packages in the above directory and load them automatically.
The third: application class loader: Application Classloader
Written in Java language and implemented by sun.misc.Launcher$AppClassLoader.
Derives from java.lang.ClassLoader, and the parent class loader is the startup class loader
It is responsible for loading the class library under the path specified by the environment variable classpath or the system property java.class.path.
It is the default class loader in the program, and all the classes in our Java program are loaded by it.
We can get and operate the loader through ClassLoader#getSystemClassLoader ()
The fourth: custom loader
In general, the above three loaders can meet our daily development work, and when not satisfied, we can also customize the loader.
For example, if the Java class is loaded on the network, the encryption operation is used to ensure the security in the transmission, so the above three loaders cannot load the class, so a custom loader is needed.
Custom loader implementation steps
Inherit the java.lang.ClassLoader class and override the findClass () method
If you don't have too complex requirements, you can directly inherit the URLClassLoader class and override the loadClass method, as described in AppClassLoader and ExtClassLoader.
Several ways to get ClassLoader
It is an abstract class, and all subsequent class loaders inherit from ClassLoader (excluding startup class loaders)
/ / method 1: get the ClassLoader clazz.getClassLoader () of the current class / / method 2: get the ClassLoader Thread.currentThread of the current thread context (). GetContextClassLoader () / / method 3: get the system ClassLoader ClassLoader.getSystemClassLoader () / / method 4: get the caller's ClassLoader DriverManager.getCallerClassLoader ()
Class loading mechanism-parent delegation mechanism
Jvm loads class files on demand, and when you need to use this class, jvm loads its class files into memory to produce class objects.
When the class is loaded, the parent delegation mechanism is used, that is, a task delegation mode in which the request is handed over to the parent class for processing.
working principle
(1) if a class loader receives a request for class loading, it will not load it first and will delegate the request to the parent class loader to execute.
(2) if there is still a parent loader in the parent class, continue to delegate upwards until the classloader is started: Bootstrap ClassLoader.
(3) if the parent class loader can complete the loading task, the successful result will be returned. If the parent class fails to load, the child class will try to load it. If the child class fails to load, a ClassNotFoundException exception will be thrown. This is the parent delegation mode.
Third-party package loading method: reverse delegation mechanism
There are many service provider interfaces (Service Provider Interface,SPI) in Java applications, which allow third parties to provide implementations for them, such as JDBC, JNDI and so on. The interfaces of these SPI belong to the Java core library and are generally stored in the rt.jar package and loaded by the Bootstrap class loader. However, the Bootstrap class loader can not directly load the implementation class of SPI, and because of the existence of parent delegation pattern, the bootstrap class loader can not reverse delegate the implementation class of AppClassLoader loader SPI. In this case, we need a special class loader to load third-party class libraries, and the thread context classloader (the destroyer of the parent delegation model) is a good choice.
From the figure, we can see that the rt.jar core package is loaded by a Bootstrap class loader, which contains SPI core interface classes. Because classes in SPI often need to call methods of external implementation classes, while jdbc.jar contains external implementation classes (jdbc.jar exists in the classpath path), they cannot be loaded through the Bootstrap class loader, so they can only delegate thread context class loaders to load the implementation classes in jdbc.jar into memory for SPI related classes to use. Obviously, the loading mode of the thread context class loader destroys the "parent delegation model". It abandons the parent delegate load chain mode during execution, so that the program can use the class loader in reverse, which also makes the Java class loader more flexible.
Sandbox security mechanism
Custom String class, but when loading the custom String class will first use the bootstrap class loader to load, while the bootstrap class loader will first load the file that comes with JDK (javalangString.class in the rt.jar package), the error message that there is no main method is because of the String class in the loaded rt.jar package. This ensures the protection of the core source code of Java, which is the sandbox security mechanism.
At this point, on the "jvm class loader, class loading mechanism is what is the end of the study, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.