当前位置:网站首页>JVM (3) class loading
JVM (3) class loading
2022-06-29 19:21:00 【Demon_ bh】
Class loading
1、 Class loading phase
1) load
- Load the bytecode of the class into the method area ( Meta space jdk1.8), Internal use C++
- If there is a parent class, load the parent class first
- Reflection is through the object head class Address found in meta space Class Class object , Get from _fields, _methods Etc !
2) Connect
1、 verification : Whether the bytecode conforms to the specification
2、 Get ready : Give class static Variable allocation space , Confirm the default value ==(static int a = 10)==
- static There are two steps to confirm the default value of variables and assign values , Confirm that the default value is completed in the preparation phase a The value is 0, The assignment is done in the initialization phase a The value is 10
- But if static Variable yes final Basic type , And string constants , Then the assignment is completed in the preparation stage
- If static A variable is final Of , But it's a reference type , The assignment will also resolve the symbol reference in the constant pool into a direct reference in the initialization phase
3、 analysis : Resolve the symbolic reference in constant pool to direct reference ( Determine the address of the reference class )
3) initialization
Initialization calls () Method , This method contains the assignment of all static variables in the class and the execution of all static code blocks , Virtual opportunity guarantees that this kind of 『 Construction method 』 Thread safety for .
* Class initialization is 【 Lazy 】:
- main The class of the method , It's always initialized first
- The first time you access a static variable or static method of this class
- Subclass initialization , If the parent class has not been initialized , May trigger
- Subclasses access static variables of the parent class , Only the initialization of the parent class will be triggered
- Class.forName
- new Will cause initialization
Does not cause class initialization :
- Access class static final static const ( Basic types and strings ) Initialization will not be triggered ( Connection phase )
- Class object .class Initialization will not be triggered ( Loading phase )
- Creating an array of this class does not trigger initialization
* The lazy singleton mode is implemented by using the class loading initialization feature
public class Singleton {
// Private constructor , Cannot be called externally
private Singleton() {
}
// Keep singletons in inner classes
private static class LazyHolder {
static final Singleton INSTANCE = new Singleton();
}
// First call getInstance Method , Will cause the inner class to load and initialize its static members
public static Singleton getInstance() {
return LazyHolder.INSTANCE;
}
}
characteristic :
- Laziness
- Thread safety during initialization is guaranteed by the class loader
2、 Class loader
- Bootstrap ClassLoader jre/lib (C++ To write , No direct access )
- Extension ClassLoader jre/lib/ext
- Application ClassLoader classpath ( system class loader )
- Custom class loaders Customize
characteristic :
- Every Class loaders have a separate class namespace
- Any class , Need to be Class itself + Load the class loader for this class Jointly determine uniqueness
** Parent delegation mode
working process : A class loader received a request to load a class , He first delegates the request to the superior class loader to complete , This is true on every floor , Until it is passed to the top-level startup class loader , Only when the upper class loader cannot complete the load request ( There is no such class in the search scope ) when , The lower loader will try to load itself .
advantage :
- Such as Object class , Its location can only be loaded by the topmost boot class loader , therefore No matter which class loader is going to load this class , Finally, it is loaded by the boot class loader , This ensures that Object Classes are the same in various classloader environments .
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, find out whether the class has been loaded by the class loader
Class<?> c = findLoadedClass(name);
// If not loaded
if (c == null) {
long t0 = System.nanoTime();
try {
// See if it has been loaded by its superior loader Extension His superior is Bootstarp, But it shows up as null
if (parent != null) {
c = parent.loadClass(name, false);
} else {
// See if it has been loaded by the startup class loader
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
// Capture exception , But do nothing
}
if (c == null) {
// If you still don't find , Let the extended class loader call findClass Method to find this class , If I still can't find , Throw an exception
// Then let the application class loader find classpath Find this class
long t1 = System.nanoTime();
c = findClass(name);
// Recording time
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
== In the load JDBC Database driven , Using the application class loader ( The boot class loader is not used , Breaking the parental delegation model )
边栏推荐
- Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
- Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
- NLP 类问题建模方案探索实践
- Win11安装权限在哪里设置?Win11安装权限设置的方法
- JS judge whether the array key name exists
- 谁在抖音文玩里趁乱打劫?
- 揭秘!付费会员制下的那些小心机!
- KDD 2022 | characterization alignment and uniformity are considered in collaborative filtering
- [software testing] 01 -- software life cycle and software development model
- 为什么信息化 ≠ 数字化?终于有人讲明白了
猜你喜欢

正则表达式系列之手机号码正则

The developer task center is online! Thousands of yuan of gifts!

C语言数组专题训练

软件工程专业大二,之前的学习情况不太好该怎么规划后续发展路线

tp5 where查询数据库中的某个字段是否包含某个值,不用like的方法,模糊查询

终于,进亚马逊了~

电脑ssd硬盘怎么安装使用

3-3 host discovery - layer 4 discovery

Point, line, surface and body of enterprise digital transformation!

AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
随机推荐
乐鑫面试流程
layer. prompt
76.二叉树的最近公共祖先
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
3-3主機發現-四層發現
Cantata 9.5版本已正式通过SGS-TÜV认证,符合所有主要软件安全标准
电脑ssd硬盘怎么安装使用
领先11%,华为云天筹AI求解器再度登顶国际权威榜单
习题8 #第8章 Verilog有限状态机设计-4 #Verilog #Quartus #modelsim
做白银k线图有多重要?
KDD 2022 | 協同過濾中考慮錶征對齊和均勻性
全局变量和静态变量的初始化
IP error problem of PHP laravel using AWS load balancer
逻辑结构与物理结构
nacos 问题
创作者基金会 6 月份亮点
jfinal中如何使用过滤器监控Druid监听SQL执行?
DAO 中存在的不足和优化方案
技术保证质量,软件测试的这些测试方法你都掌握了吗?
layer.prompt