当前位置:网站首页>JVM class loader; Parental delegation mechanism
JVM class loader; Parental delegation mechanism
2022-06-11 21:28:00 【z754916067】
Catalog
Classloader subsystem
The classloader subsystem is responsible for loading from the file system or network class file ,class The file has a specific file ID at the beginning of the file .
The loading process of a class
- Loading phase (Loading)
- Link phase (Linking)
- Initialization phase (Initialization)
Deposit
The loaded class information is stored in a block called Method area Of memory space . In addition to class information , The method area also holds runtime constant pool information , It may also include string literals and numeric constants .
Stage 1:Loading
- Get the binary byte stream that defines a class by its fully qualified name .
- Convert the static storage structure represented by the byte stream into the runtime data structure of the method area .
- Generate a representation of this class in memory java.lang.Class object , As the access to all kinds of data of this class in method area .
Stage 2:Linking
verification
Make sure class The information contained in the byte stream of the file meets the requirements of the current virtual machine , Ensure the correctness of the loaded class , Does not harm the security of virtual machine itself .
Get ready
Class variables ( Static variables ) Allocate memory and Set the default initial value of this kind of variable , That's zero .
There is no use for final Embellished static, because final It will be allocated at compile time .
analysis
The process of converting a symbolic reference in a constant pool to a direct reference .
Stage 3:Initialization
The initialization phase is the execution of class constructor methods < clinit>() The process of . This method is used to collect all class variables in the class ( That is, static variables ) The assignment action of is combined with the statement in the static code block .
If there are no static code blocks and static variables , This method will not call .
Classloader classification
- Bootstrap ClassLoader( Boot class loader ,c and c++ Realization )
- Extension ClassLoader( Extend the classloader , Inherited from ClassLoader, So it is a custom class loader ,java Realization )
- System ClassLoader( Inherited from ClassLoader, So it is a custom class loader ,java Realization )
Code
// Get system classloader
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader);
// Get the top : Extend the classloader
ClassLoader extCLassLoader = systemClassLoader.getParent();
System.out.println(extCLassLoader);
// Get the top Print out null
ClassLoader bootstrapClassLoader = extCLassLoader.getParent();
System.out.println(bootstrapClassLoader);
// Get user-defined class loader by AppClassLoader and systemClassLoader It's the same
ClassLoader classLoader = test.class.getClassLoader();
// Print as null The explanation is bootstrapClassLoader That is, the core class uses this kind of loader
ClassLoader classLoader1 = String.class.getClassLoader();
Start class loader
Bootstrap ClassLoader, Use C/C++ Language implementation , No parent loader
Application class loader
AppClassLoader, The parent class loader is the extension class loader , Responsible for loading environment variables classpath Or system properties , Is the default class loader in the program .
ClassLoader class
ClassLoader Class is an abstract class , All subsequent classloaders are inherited from ClassLoader( Does not include boot loader )
API
obtain ClassLoader:
- Get the... Of the current class ClassLoader:getClassLoader()
- Gets the context of the current thread ClassLoader:Thread.currentThread().getContextClassLoader()
- Get the... Of the current system ClassLoader:ClassLoader.getSystemClassLoader()
- Gets the caller's classLoader:DriverManager.getCallerClassLoader()
Parent delegate mechanism
Java Virtual machines are right class The file is loaded on demand , That is to say, the class will be used only when it is needed class File loading into memory generation class object .
And load the class When you file ,Java The virtual machine adopts the mode of parent delegation , That is, leave the request to the parent class , It's a task delegation model .
working principle
- If a class loader receives a class load request , It doesn't load itself first , Instead, the request is delegated to the loader of the parent class .
- If the parent loader still has its parent loader , Then further entrust , Recursion in turn , Finally, you will reach the top-level startup class loader .
- If the parent loader can complete the class loading task , You're back , If this load task cannot be completed , Sub loader will try to load by itself .
advantage
- Avoid duplicate loading of classes
- Protect program security , Prevention core API Be tampered with at will
Sandbox security mechanism
Java The core of security mode is Java The sandbox (sandbox), Sandbox is an environment that limits the running of programs . Sandbox mechanism is to make Java Code is limited to virtual machines (JVM) Within a specific operating range , And strictly restrict code access to local system support , Through such measures , To ensure effective code isolation and prevent damage to the local system .
Sandbox mainly restricts access to system resources , Include CPU、 Memory 、 file system 、 The Internet . Different levels of sandboxes can access these resources differently .
Sandbox basic components
- bytecode verifier (bytecode verifier): Make sure Java Class files follow Java language norm . This can help Java Program to achieve memory protection . But not all class files are byte checked , For example, core classes .
- Class loader (class loader): The class loader is in 3 In every respect Java Sandbox works
1. It prevents malicious code from interfering with well meaning code ;// Parent delegate mechanism , Yes java Protect the core source code
2. It guards the boundaries of trusted class libraries ;
3. It guards the boundaries of trusted class libraries ;
4. He puts the code into a protected domain , Determines what the code can do .
Other
Judge class Whether the objects are the same
stay JVM The middle represents two class There are two necessary conditions for an object to be the same class :
1. The complete class name of a class must be consistent , Include package name .
2. Load the ClassLoader( finger ClassLoader Instance object ) It has to be the same .
Reference to classloader
JVM You must know whether a type is loaded by the boot loader or the user class loader .
If a type is loaded by a user class loader , that JVM A reference to the class loader is saved in the method area as part of the type information .
When resolving a reference from one type to another ,JVM You need to ensure that the two types of class loaders are the same .
Active and passive use of class
The active use of classes will lead to class initialization , Passive use of classes does not .
Active use
- Create class instances
- Accessing static variables of a class or interface , Or assign a value to the static variable
- Calling static methods of a class
- Reflection
- Initializing a subclass of a class .
- JVM Classes marked as startup classes at startup .
边栏推荐
- Bipartite King
- 作为一名 ABAP 资深顾问,下一步可以选择哪一门 SAP 技术作为主攻方向?
- Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
- 八、BOM - 章节课后练习题及答案
- Goland中在文件模板中为go文件添加个人声明
- Goto statement of go language
- The gateway starts other microservices first. When the gateway is started, the gateway cannot be started and there is no exception log; Start the gateway first, and all services can be started normall
- 12 golden rules of growth
- 實驗10 Bezier曲線生成-實驗提高-控制點生成B樣條曲線
- 常用文件函数
猜你喜欢

Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience

Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience

Codeforces Round #744 (Div. 3) 解题报告
![[advanced C language] integer storage in memory](/img/a5/6c7df3b8f427fe724922a6b853516f.png)
[advanced C language] integer storage in memory

Deriving Kalman filter from probability theory

LeetCode-104-二叉树的最大深度

Cs144 lab0 lab1 record

Jenkins+allure integrated report construction

JVM method area

Tensorflow 2. X Getting Started tutorial
随机推荐
apache 本地多端口配置
Expérience 10 génération de courbes bezier - amélioration expérimentale - génération de courbes B - spline par point de contrôle
Realize the same length of tablayout subscript and text, and change the selected font size
【C语言进阶】整型在内存中的存储
Educational codeforces round 111 (rated for Div. 2) C Supplement
Common file functions
关于斜率优化
【C語言進階】整型在內存中的存儲
Object creation process of JVM
Using the sap ui5 cli command line tool to build and run SAP ui5 applications
[Part 14] source code analysis and application details of completionservice class [key]
Solve the problem of img 5px spacing
Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:
How to create the simplest SAP kyma function
Codeforces Round #742 (Div. 2) F. One-Four Overload
Flutter implements the JD address selection component
Why microservices are needed
String copy function
Website online customer service system Gofly source code development log - 2 Develop command line applications
A collection of commonly used open source data sets for face recognition