如何检查OSGi包中的类是否已正确卸载? [英] How to check if a class in an OSGi bundle has been correctly unloaded?
问题描述
我有一个应用程序,我怀疑它泄露了一些类的引用。
为了暴露这种错误的行为,我们写了一个测试: BundleUnloadIT
在这个测试中,我使用
Assertions.assertThat(weakReference.isEnqueued() )
.as(弱引用应该被排入队列,但不是)isTrue();
测试我的引用是否正确。
但是无论我尝试做什么,参考都不会排队。
所以我怀疑这个类的某些引用正在泄漏某处。
所以...我用正确的方法来测试我的类是否被卸载了?
如果不是的话,我该如何正确地做这个测试? 如果是,那里有一个参考泄漏某处。我怎么才能找到它?
如果类的类加载器不强烈引用(也就是说,类的类加载器也可以被垃圾收集)。如果所有加载的类都没有强引用,则类加载器只能被垃圾回收。所以这是一个全有或全无的主张。每个类都对其类加载器有强烈的参考,每个类加载器都强烈地引用了它所加载的所有类。所以如果任何类加载的类被强引用,那么就不可能收集垃圾。
I have an application which I suspect to leak some class references.
To expose that buggy behaviour, we wrote a test : BundleUnloadIT
In that test, I use
Assertions.assertThat(weakReference.isEnqueued())
.as("weak reference should have been enqueued, but is not").isTrue();
To test if my reference is correctly gc'ed. But whatever I try to do, the reference is never enqueued. So I suspect some reference to that class is leaking somewhere.
So ... am I using the right method to test my class is unloaded ?
And if not, how can I properly do that test ?
And if it is, there is a reference leaked somewhere. how can I find it ?
A class can only be unloaded (garbage collected) if the class' class loader is not strongly referenced (that is, the class' class loader can also be garbage collected). And a class loader can only be garbage collected if all of the classes it loaded are not strongly referenced. So it is an all-or-nothing proposition. Each class has a strong reference to its class loader and each class loader has strong references to all the classes it has loaded. So it is not possible for a class to be garbage collected if any class loaded by the class loader is strongly referenced.
这篇关于如何检查OSGi包中的类是否已正确卸载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!