当前位置:网站首页>JVM class loading subsystem
JVM class loading subsystem
2022-07-06 17:14:00 【Smart popcorn】
1. Class loader and class loading process
Let's look at a picture first , Bytecode files go through the process of class loading system , First of all, we need to be clear about , What is the role of a class loader ?
It mainly plays a role of transmission , Equivalent to a courier , The later process can be regarded as an assembly line , The corresponding documents will be verified , Judge whether it is legal .
Class loading process
load
The loading here is only the initial process of the whole loading , This is something to be aware of , Loading is mainly divided into three steps .
1. Get the binary byte stream of the corresponding class through the fully qualified class name of the class .
2. Convert the static structure represented by byte stream into the runtime structure of method area .
3. Generate the corresponding... In memory java.lang.Class object .
link
The link is broken down into three steps , These three steps are very important .
verification
Someone may change it maliciously class file , Achieve some kind of attack , So we should verify it first .
Get ready
- Assign a value of zero to the class variable
- Add final The variables of are defined at compile time , So no assignment
- At this time, the object has not been created , So instance variables will not be assigned
analysis
Convert symbolic references to direct references , The specific details of bytecode will be discussed again .
initialization
First of all, understand clint Methods and init Method , The former is assigned by all class variables , Methods for merging static statements , The latter has several constructors , There will be several corresponding init Method .
clint The method is to use static Embellished sentences , Will be added to it , If a class has a parent , Then the parent class will be executed first clinit Method .
init Methods contain constructors , Explicitly initialize , Such as direct assignment , Code block , Instantiation is included .
The whole initialization phase is execution clinit Methodical , And it's executing clinit Method will lock , Ensure data security .
2. Classloader classification
Class loaders are mainly divided into two categories , One is C language-written , go by the name of Boot class loader , Others can be counted as custom loaders , The details are as follows .
The top layer is the most special , The rest is made up of Java The realization of language .
You can run the following code , Try to get the next class loader .
public class Test {
public static void main(String[] args) {
// Get system classloader
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader);//sun.misc.Launcher$ [email protected]
// Get extended class loader
ClassLoader extClassLoafer = systemClassLoader.getParent();
System.out.println(extClassLoafer);//sun.misc.Launcher$ [email protected]
// User defined class loader
ClassLoader classLoader = Test.class.getClassLoader();
System.out.println(classLoader);//sun.misc.Launcher$ [email protected]
// Try to get the boot class loader
ClassLoader bootstrapClassloader = extClassLoafer.getParent();
System.out.println(bootstrapClassloader);//null
}
}
You can see , We found that , The custom class loader is the system class loader , The upper level of the system class loader is the boot class loader , The upper level of the boot class loader is null, the reason being that C Written language , We can't get .
3. ClassLoader Instructions for use
Bootstrap classLoader
Boot class loader , Also known as boot class loader , The role of the most superior , use C Language writing , Used to load some core classes , Such as JAVA_HOME/jre/lib/rt.jar、resource.jar, Provide JVM The class needed by itself .
Extension classLoader
Extend the classloader , Derive from ClassLoader class , from Java Language writing , Load content other than the core package , Such as user built Jar bag jre/lib/ext Under the table of contents , It will also be loaded by the extension class loader .
AppClassLoader
system class loader , Generally speaking, the classes we define are loaded by it , It is also the default loader in the program .
In some cases , We really need custom loaders , Such as isolation loading class , Modify the loading method of avocado , Prevent source code leakage , The details will be discussed later .
4. Talk about the parental delegation mechanism
I was asked about this by the interviewer , In fact, I don't think this translation is accurate , It should be called parent agent , Parents don't mean parents , It refers to the higher level of the loader .
The flow chart is shown below .
The specific process is divided into three steps .
1. When a class receives a load request , It doesn't load directly , Instead, it is executed by its parent class loader
2. If there is a parent loader , Just entrust upward , Until you know your superiors
3. If the top-level loader can finish loading, it is certainly best , If not , Then it is handled by the subclass loader .
You can imagine a delicious meal , Although children want to eat , But first push it to Dad , Dad wants to , Grandpa hasn't moved his chopsticks yet , Wait until grandpa eats , Grandpa said , This thing is too harsh , You eat it , Dad ate it first ......
Let's give you an example , Define a java.lang Under bag Hello class
But the operation directly reported an error , Security exception , Forbidden package name java.lang
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.lang
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:662)
at java.lang.ClassLoader.defineClass(ClassLoader.java:761)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
Because the classes under this package are managed by the boot class loader , If it can be defined like this , So if you write a dead circle , Or delayed procedures , It may cause the loader to run abnormally , This is also a protective measure .
5. other
Interview questions : If you judge two class Whether the object is the same class ?
First, the complete package names of the two classes should be consistent , And their loaders should also be consistent , Only when you answer the second point can you distinguish yourself from others .
Active and passive use of classes
Active reference will lead to class initialization , You can recall the first section , That is, whether it will be implemented clinit Method .
There are seven
- Create an instance of a class
- Accessing a class / Static variables of the interface , Or assign a value to the static variable
- Calling static methods of a class
- Reflection get class
- JVM The class that is indicated to be started at startup ( Remember this first I haven't met )
- JDK 7 Start with dynamic language support :
java.lang.invoke.MethodHandle The parsing result of the instance
REF_getStatic、REF_putStatic、REF_invokeStatic The class corresponding to the handle is not initialized ( Understanding can )
The last two are rarely encountered , Understanding can .
Share daily
Some people are shallow , Some people's gold and jade watch is full of corruption . One day you will meet a person as gorgeous as a rainbow , When you meet this person , Others are just clouds .
边栏推荐
- Introduction to spring trick of ByteDance: senior students, senior students, senior students, and the author "brocade bag"
- After idea installs the plug-in, restart the plug-in and disappear
- redux使用说明
- koa中间件
- Which is more important for programming, practice or theory [there are some things recently, I don't have time to write an article, so I'll post an article on hydrology, and I'll fill in later]
- The daemon thread starts redis and modifies the configuration file
- Take you hand-in-hand to do intensive learning experiments -- knock the level in detail
- 连接局域网MySql
- Instructions for Redux
- 控制转移指令
猜你喜欢
Shell_ 04_ Shell script
Alibaba cloud server builds SVN version Library
Shell_ 06_ Judgment and circulation
Koa Middleware
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
服务器端渲染(SSR)和客户端渲染(CSR)的区别
MySQL数字函数
Ruoyi-Cloud 踩坑的BUG
Idea breakpoint debugging skills, multiple dynamic diagram package teaching package meeting.
Take you hand-in-hand to do intensive learning experiments -- knock the level in detail
随机推荐
Shell_ 06_ Judgment and circulation
Resume of a microservice architecture teacher with 10 years of work experience
JVM之垃圾回收器上篇
Some instructions on whether to call destructor when QT window closes and application stops
Only learning C can live up to expectations top3 demo exercise
The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games
Activiti目录(一)重点介绍
DS18B20數字溫度計系統設計
服务器端渲染(SSR)和客户端渲染(CSR)的区别
MySQL optimization notes
Von Neumann architecture
唯有学C不负众望 TOP3 Demo练习
DOS function call
Idea resolving jar package conflicts
MySQL数字函数
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
连接局域网MySql
Instructions for Redux
Shell_ 04_ Shell script
Ruoyi-Cloud 踩坑的BUG