处置类加载器
我正在使用扩展 URLClassLoader 的自定义类加载器。我将一些类加载到自定义类加载器中并执行一些任务。任务完成后我想处理类加载器。我尝试通过将引用设置为空来做到这一点。
但这不会对类加载器进行垃圾收集。
有没有一种方法可以帮助我实现我想要的目标?
I am using a custom class loader which extends URLClassLoader. I load some classes into my custom class loader and perform some task. Once the task is completed i want to dispose of the class loader. I tried doing that by setting the reference to null.
But this does not garbage collect the class loader.
Is there a way that can help what i want to achieve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
基本上,正如 @invariant 已经指出的那样,取消引用特定类加载器加载的所有类应该使该类加载器可被垃圾回收。但是,(至少)有一个例外:如果一个类被序列化,则该类(及其类加载器)将由
ObjectStreamClass
内部引用,这是一个原始类,因此永远不会被垃圾收集。所以在这种情况下,类加载器也不能被垃圾回收,直到整个 JVM 终止。请参阅此处的完整说明,在“与垃圾收集和序列化相关的问题”部分中。
Basically, as @invariant already pointed out, dereferencing all classes loaded by the specific classloader should make that classloader garbage collectable. However, there is (at least) one exception: if a class is serialized, that class (and thus its classloader) is kept referenced internally by
ObjectStreamClass
, which is a primordial class and therefore is never garbage collected. So in this case, the classloader can not be garbage collected either until the whole JVM terminates.See the full explanation here, in the section "Problems related to garbage collection and serialization".
来自 ClassLoader 文档:
每个 Class 对象都包含对定义它的 ClassLoader 的引用
。这会阻止您的加载程序被收集。您还必须取消对类和这些类的实例的所有引用。From the ClassLoader doc:
Every Class object contains a reference to the ClassLoader that defined it
. This is preventing your loader being collected. You would have to null out all references to the classes and instances of those classes too.https://bugs.java.com/bugdatabase 有一个 6 年前的错误/view_bug?bug_id=4950148 这似乎就是你想要的。不幸的是,似乎还没有实现这样的功能......
there's a 6-year-old bug at https://bugs.java.com/bugdatabase/view_bug?bug_id=4950148 that seems to be what you want. unfortunately no such functionality seems to have been implemented yet...
当 ClassLoader 不再创建类的实例时,该 ClassLoader 将被垃圾回收。有一个旧错误(4950148)与没有办法明确地相关处理类加载器,这会导致文件锁定等问题。
此问题现已在 JDK 7 中修复它添加了 URLClassLoader.close() 方法。
The ClassLoader will be garbage collected after there are no more instances of classes created by the ClassLoader. There is an old bug (4950148) related to there being no way to explicitly dispose of a ClassLoader, which causes problems for example with file locking.
This has now been fixed in JDK 7 which adds an URLClassLoader.close() method.
只要引用了该类加载器加载的任何类,该类加载器就不会被垃圾回收。因此,如果出现以下情况,类加载器将被删除: 对类加载器的所有直接引用都为空,对此类加载器加载的类以及这些类的实例的所有引用。然后它可以在垃圾收集器下次运行时转储。
As long as any of the classes loaded by this classloader is referenced, the classloader will not be garbage-collected. So the classloader will be removed if: all direct references to the classloader are nulled, all references to classes loaded by this classloader and to instances of these classes. Then it can be dumped on the next run of the garbage-collector.