Java – How do I debug a memory leak where an exception instance in a heap dump has no inbound references?

How do I debug a memory leak where an exception instance in a heap dump has no inbound references?… here is a solution to the problem.

How do I debug a memory leak where an exception instance in a heap dump has no inbound references?

I’ve been trying to diagnose a memory leak in the Android app I’m writing. I loaded the heap dump into Eclipse, but I see very strange results. There are about 20,000 exception instances in the heap (specifically, LDAPException from the UnboundID LDAP library) with no inbound references.

That is, they appear at the root of the dominance tree. OQL SELECT objects e FROM com.unboundid.ldap.sdk.LDAPException e WHERE (inbounds(e).length = 0)returns more than 20,000 results, totaling almost all of the heap. However, GC runs before the heap dump and I can see it running repeatedly in the console during the execution of the leaking code. If these instances don’t have inbound references, what keeps them active?

I also tried to do the “shortest path to GC” query. It shows that an LDAPConnectionReader row holds 2 instances, and ~20k LDAPException @ <addr> unknownrows with various hexadecimal addresses.

Update: I haven’t had time for further diagnosis since the release, and the bounty I posted may end early. I now reward it as much as possible so as not to waste points. Thank you to everyone who investigated this matter! When life is a little less busy, I will come back again to update the results of further diagnosis.

Best Solution

Whether or not these exceptions are thrown, that detail is almost irrelevant in terms of memory usage.

While you want to see who owns the reference in the heap dump, for some reason you can’t do that. I wonder if the native code will be properly symbolized in the heap dump tool?

Either way, as a new thing to try, I recommend debugging not the points where these exceptions are thrown, but the points they create. Place breakpoints on the class and/or all its constructors. Ideally, you would just get this information from the heap dump reference, but if you can see who is building these objects repeatedly, it might still provide useful information… I guess they came from the same place.

Related Problems and Solutions