Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find GC roots of Anonymous Classes in java?

Just today I discovered memory leak in my app with large number of objects created as anonymous classes. It was classic example that each object had it's own class defined and put into heap.

It wasn't trivial to find in runtime memory profiler and heap dump analysis done by Eclipse Memory Analyzer wasn't helpful neither.

I was wondering how to log all anonymous classes created in runtime with thread id (or at least parent classloader).

I think this single thing alone would make finding root cause trivial.

like image 576
Petro Semeniuk Avatar asked Dec 08 '15 11:12

Petro Semeniuk


1 Answers

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

What you probably need is logic to search for instances of classes that are retained by inner classes references (which include anonymous classes).

In JProfiler, there is a corresponding inspection:

enter image description here

The object set create by the inspection contains all such instances and you can then show a paths to a GC root:

enter image description here

Disclaimer: My company develops JProfiler.

like image 104
Ingo Kegel Avatar answered Oct 07 '22 22:10

Ingo Kegel