gpt4 book ai didi

java - 卸载类加载器

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:13:51 24 4
gpt4 key购买 nike

我的最终目标是能够在类已经加载到 JVM 后重新加载它们。

看完下面的回答Unloading classes in java? ,我一直在尝试实现我自己的类加载器,它本身为它加载的每个类创建一个不同的类加载器实例(与它自己的类型相同)。

因此,结果是每个类加载器一个类。

目的是能够对类进行 GC,即它的所有实例,然后卸载它的类加载器,并能够从它的字节中重新加载相同的类。

问题是 - 我可以看到我的类实例正在使用 finalize() 方法进行垃圾回收,但我无法让我的类加载器卸载或进行垃圾回收。
是否有任何代码示例,一个简单的测试,说明如何完成?

谢谢,任何帮助将不胜感激

编辑:

更清楚一点,我对新对象的实例化是通过“new()”操作数进行实例化的代码示例感兴趣,并且类加载器没有显式地重新加载主类,但是在调用下一个“new()”之后。

最佳答案

如果没有更多的引用,类加载器应该被垃圾回收。我拍了this code来自@PeterLawrey(谢谢)(它做的和你的一样),在自定义类加载器 finalize() 方法中放置一个日志,瞧,类加载器在它们加载的类是 gc 之后被垃圾收集:

 /* Copyright (c) 2011.  Peter Lawrey
*
* "THE BEER-WARE LICENSE" (Revision 128)
* As long as you retain this notice you can do whatever you want with this stuff.
* If we meet some day, and you think this stuff is worth it, you can buy me a beer in return
* There is no warranty.
*/


import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLClassLoader;

public class LoadAndUnloadMain {
public static void main(String... args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InterruptedException {
URL url = LoadAndUnloadMain.class.getProtectionDomain().getCodeSource().getLocation();
final String className = LoadAndUnloadMain.class.getPackage().getName() + ".UtilityClass";
{
ClassLoader cl;
Class clazz;

for (int i = 0; i < 2; i++) {
cl = new CustomClassLoader(url);
clazz = cl.loadClass(className);
loadClass(clazz);

cl = new CustomClassLoader(url);
clazz = cl.loadClass(className);
loadClass(clazz);
triggerGC();
}
}
triggerGC();
}

private static void triggerGC() throws InterruptedException {
System.out.println("\n-- Starting GC");
System.gc();
Thread.sleep(100);
System.out.println("-- End of GC\n");
}

private static void loadClass(Class clazz) throws NoSuchFieldException, IllegalAccessException {
final Field id = clazz.getDeclaredField("ID");
id.setAccessible(true);
id.get(null);
}

private static class CustomClassLoader extends URLClassLoader {
public CustomClassLoader(URL url) {
super(new URL[]{url}, null);
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
try {
return super.loadClass(name, resolve);
} catch (ClassNotFoundException e) {
return Class.forName(name, resolve, LoadAndUnloadMain.class.getClassLoader());
}
}

@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println(this.toString() + " - CL Finalized.");
}
}
}

class UtilityClass {
static final String ID = Integer.toHexString(System.identityHashCode(UtilityClass.class));
private static final Object FINAL = new Object() {
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println(ID + " Finalized.");
}
};

static {
System.out.println(ID + " Initialising");
}
}

关于java - 卸载类加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16307149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com