Reputation: 704
I have the application which creates Class Loader instance (URLClassLoader) and loads dynamically Jar file using it. Next, an instance of class from Jar is created using reflection. Finally, all references are nullified in the way that Class Loader and its related objects and classes are supposed to be garbage-collected.
During profiling in JProfiler 10.1.5, I click "Run GC" toolbar button and then I take heap snapshot (does not matter whether "Perform full GC in heap snapshot" option is selected). As I expected, there is no Class Loader instance on the heap, no related objects and classes, they ware garbage collected.
On the other hand, when I do not use "Run GC" button, but take many heap snapshots with "Perform full GC in heap snapshot" option selected (with "Retain objects held by strong references only" sub-option), I always see my Class Loader instance on the snapshot, along with related objects and classes. They were not garbage collected! Why?
Upvotes: 1
Views: 339
Reputation: 48090
A class loader is considered a GC root by JProfiler, the heap walker will not try to collect class loaders.
Upvotes: 1