当前位置:网站首页>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 .
边栏推荐
- After idea installs the plug-in, restart the plug-in and disappear
- 一个数10年工作经验的微服务架构老师的简历
- Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?
- In the command mode in the VI editor, delete the character usage at the current cursor__ Command.
- Some instructions on whether to call destructor when QT window closes and application stops
- 吴军三部曲见识(四) 大家智慧
- Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance
- Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.
- Activit零零碎碎要人命的坑
- JVM垃圾回收概述
猜你喜欢
IDEA断点调试技巧,多张动图包教包会。
JVM运行时数据区之程序计数器
Assembly language addressing mode
手把手带你做强化学习实验--敲级详细
Activiti directory (IV) inquiry agency / done, approved
JVM之垃圾回收器下篇
数据传送指令
汇编课后作业
Flink 解析(四):恢复机制
Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
随机推荐
Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance
"One year after graduation, I won ACL best paper"
JS garbage collection mechanism and memory leakage
High performance mysql (Third Edition) notes
Some feelings of brushing leetcode 300+ questions
ByteDance technical Interviewer: what kind of candidate do I want to pick most
Description of project structure configuration of idea
Shell_ 07_ Functions and regular expressions
Activit零零碎碎要人命的坑
Logical operation instruction
一个数10年工作经验的微服务架构老师的简历
mysql 基本增删改查SQL语句
Take you hand-in-hand to do intensive learning experiments -- knock the level in detail
Von Neumann architecture
QT system learning series: 1.2 style sheet sub control lookup
Assembly language addressing mode
Shell_ 03_ environment variable
汇编语言段定义
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
關於Stream和Map的巧用