How to check if a class in an OSGi bundle has been correctly unloaded?

牧云@^-^@ 提交于 2019-12-12 05:52:19

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/31988047/how-to-check-if-a-class-in-an-osgi-bundle-has-been-correctly-unloaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!