当前位置:网站首页>Recycling of classes loaded by classloader
Recycling of classes loaded by classloader
2022-07-28 08:39:00 【Kuangxuefeng】
Under what circumstances , The classes in the method area will be recycled ?
1. All instance objects corresponding to this class have been recycled ,Java The instance object of this class no longer exists in the heap .
2. Load the class loader of this class ClassLoader It's been recycled .
3. Of the class Class Object has no references .About the understanding of the second sentence , Load the class loader of this class ClassLoader It's been recycled , Not just currently created ClassLoader It's recycled , But to really load this classClassLoader Recycled ( Save the current class Of ProtectionDomain It's recycled ), The current class will be recycled ; This is related to the parental delegation mechanism !
demo as follows
package com.kxf.test;
import java.lang.reflect.Method;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.security.cert.Certificate;
public class CustomClassLoader extends ClassLoader {
// TestHelloWorld Class name
public static final String testClassName = "com.anbai.sec.classloader.TestHelloWorld";
public static final String customLoaderStaticBean = "com.kxf.test.CustomLoaderStaticBean";
// TestHelloWorld Class bytecode
private static final byte[] testClassBytes = new byte[] { -54, -2, -70, -66, 0, 0, 0, 51, 0, 17, 10, 0, 4, 0, 13, 8, 0,
14, 7, 0, 15, 7, 0, 16, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100,
101, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 5, 104, 101, 108,
108, 111, 1, 0, 20, 40, 41, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103,
59, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 19, 84, 101, 115, 116, 72, 101, 108, 108,
111, 87, 111, 114, 108, 100, 46, 106, 97, 118, 97, 12, 0, 5, 0, 6, 1, 0, 12, 72, 101, 108, 108, 111, 32, 87,
111, 114, 108, 100, 126, 1, 0, 40, 99, 111, 109, 47, 97, 110, 98, 97, 105, 47, 115, 101, 99, 47, 99, 108,
97, 115, 115, 108, 111, 97, 100, 101, 114, 47, 84, 101, 115, 116, 72, 101, 108, 108, 111, 87, 111, 114, 108,
100, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 0, 33, 0, 3, 0, 4, 0,
0, 0, 0, 0, 2, 0, 1, 0, 5, 0, 6, 0, 1, 0, 7, 0, 0, 0, 29, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0,
0, 1, 0, 8, 0, 0, 0, 6, 0, 1, 0, 0, 0, 7, 0, 1, 0, 9, 0, 10, 0, 1, 0, 7, 0, 0, 0, 27, 0, 1, 0, 1, 0, 0, 0,
3, 18, 2, -80, 0, 0, 0, 1, 0, 8, 0, 0, 0, 6, 0, 1, 0, 0, 0, 10, 0, 1, 0, 11, 0, 0, 0, 2, 0, 12 };
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
// Only deal with TestHelloWorld class
if (name.equals(testClassName)) {
ProtectionDomain pd = new ProtectionDomain(new CodeSource(null, (Certificate[]) null),
null, this, null){
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
System.out.println("CustomClassLoader ProtectionDomain finalize===");
}
};
// call JVM Of native Method definition TestHelloWorld class
return defineClass(testClassName, testClassBytes, 0, testClassBytes.length, pd);
}
return super.findClass(name);
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
System.out.println("CustomClassLoader finalize===");
}
}
package com.kxf.test;
import java.lang.ref.WeakReference;
public class ClassLoaderTest {
static WeakReference<Class> reference = null;
static WeakReference<Object> referenceObj = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
ClassLoader mClassLoader = new CustomClassLoader(){
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
System.out.println("mClassLoader finalize");
}
};
// referenceObj = new WeakReference<Object>(new Object());
try {
Class<?> mClass = mClassLoader.loadClass("com.kxf.test.ClassLoaderBean");
reference = new WeakReference<Class>(mClass);
Object obj = mClass.newInstance();
System.out.println("obj=="+obj);
System.out.println("mClass getClassLoader=="+mClass.getClassLoader());
System.out.println("mClass=="+mClass);
Class testClass = mClassLoader.loadClass(CustomClassLoader.testClassName);
System.out.println("testClass=="+testClass);
System.out.println("testClass getClassLoader=="+testClass.getClassLoader());
// Reflection to create TestHelloWorld class , Equivalent to TestHelloWorld t = new TestHelloWorld();
Object testInstance = testClass.newInstance();
referenceObj = new WeakReference<Object>(testInstance);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.gc();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("reference=="+reference.get());
System.out.println("referenceObj=="+referenceObj.get());
}
});
thread.start();
}
}
The output log is as follows :
ClassLoaderBean static===
[email protected]
mClass [email protected]531d72ca
mClass==class com.kxf.test.ClassLoaderBean
testClass==class com.anbai.sec.classloader.TestHelloWorld
testClass [email protected]
CustomClassLoader ProtectionDomain finalize===
CustomClassLoader finalize===
mClassLoader finalize
ClassLoaderBean finalize===
reference==class com.kxf.test.ClassLoaderBean
referenceObj==nullFrom the above, we can analyze ,mClassLoader Although it was recycled , But I can see ClassLoaderBean It's not recycled , as a result of ClassLoaderBean It's basically AppClassLoader Loaded , The specific reason is ClassLoader Parental delegation model ;testClass Will be recycled , The reason is generated ProtectionDomain It's just CustomClassLoader Inside , That is, basically load testClass Of ClassLoader Namely mClassLoader In itself .
边栏推荐
- Creation of status bar (29)
- Gbase appears in Unicom cloud Tour (Sichuan Station) to professionally empower cloud ecology
- Does gbase 8s support storing relational data and object-oriented data?
- 解决EMC、EMI传导干扰的八大方法
- pyspark 写入数据到iceberg
- Use of namespaces
- HCIP---LDP和MPLS技术(详解)
- pyspark更改列顺序存入iceberg数据库
- File operation of QT
- 5张图告诉你:同样是职场人,差距怎么这么大?
猜你喜欢

Hcip --- LDP and MPLS Technology (detailed explanation)

2022牛客多校第一场解题报告

五张图看懂EMI电磁干扰的传播过程-方波陡峭程度对高频成分的影响,时序到频域频谱图形,波形形状对EMI辐射的影响。

EMC EMI磁珠的特性

GBase 8a MPP与银河麒麟(x86版)完成深度适配

Leetcode brushes questions. I recommend this video of the sister Xueba at station B

Js继承方法

One key switch circuit

uniapp的swiper动态设置current值不生效解决办法

Shell编程规范与变量
随机推荐
HCIP第八天
[mindspire YiDianTong robot-01] you may have seen many Knowledge Q & A robots, but this is a little different
博客搭建七:hugo
GBase 8s是否支持存储关系型数据和对象型数据?
网络安全漏洞分析与漏洞复现
Window 2 - > toolbar (28-1)
ciou损失
一键开关机电路
【软考软件评测师】2013综合知识历年真题
学术界爆火的类脑智能,啥时候能落地?来听行业大咖怎么说丨量子位·对撞派 x 时识科技...
Win the bid! Nantah general gbase 8s won the bid for the 2022 database framework project of NARI Group
Sparksql and flinksql create and link table records
The current value of uniapp's swiper dynamic setting does not take effect solution
Opencv+paddle Orc recognize pictures and extract table information
How to set it to pop up the right-click menu
49-OpenCv深入分析轮廓
leetcode/单词长度的最大乘积
2021-07-02
2022牛客多校第二场解题报告
EMC EMI磁珠的特性