How to find the GC roots of Anonymous classes in java?

Only today I discovered a memory leak in my application with a large number of objects created as anonymous classes. A classic example was that each object had its own class and was placed in a heap.

It was not easy to find the runtime in the memory profile, and the heap dump analysis performed by Eclipse Memory Analyzer did not help either.

I was wondering how to write all anonymous classes created at runtime with a thread id (or at least with a parent class loader).

I think that this one thing would make finding the root cause trivial.

+6
source share
1 answer

I'm not sure that listing all the anonymous classes will take you anywhere, there are usually a lot of them.

You might need some logic to look for instances of classes that are stored by references to inner classes (which include anonymous classes).

JProfiler has a corresponding check:

enter image description here

The set of objects created during the scan contains all such instances, and you can then show the paths to the GC root:

enter image description here

Disclaimer: My company is developing JProfiler.

+1
source

All Articles