当前位置:网站首页>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 .
边栏推荐
- Some feelings of brushing leetcode 300+ questions
- 8086 CPU 内部结构
- Activiti目录(五)驳回、重新发起、取消流程
- 8086 CPU internal structure
- 8086 segmentation technology
- 唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符
- Only learning C can live up to expectations Top1 environment configuration
- 汇编课后作业
- GCC error: terminate called after throwing an instance of 'std:: regex_ error‘ what(): regex
- 汇编语言段定义
猜你喜欢

Instructions for Redux

Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)

Activiti目录(五)驳回、重新发起、取消流程

Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.

TCP的三次握手和四次挥手

redux使用说明

Shell_ 06_ Judgment and circulation

JVM garbage collector part 2

Flink源码解读(三):ExecutionGraph源码解读

字节跳动技术面试官现身说法:我最想pick什么样的候选人
随机推荐
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
Flink 解析(六):Savepoints
Activiti目录(五)驳回、重新发起、取消流程
MySQL date function
Only learning C can live up to expectations Top1 environment configuration
程序员定位解决问题方法论
算数运算指令
Wu Jun trilogy insight (IV) everyone's wisdom
Data transfer instruction
Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?
Introduction to spring trick of ByteDance: senior students, senior students, senior students, and the author "brocade bag"
Log4j2 major vulnerabilities and Solutions
唯有学C不负众望 TOP1环境配置
8086 内存
mysql 基本增删改查SQL语句
控制转移指令
Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021
Koa Middleware
Coursera cannot play video
vscode