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.