I am looking into how to use JConsole
to detect memory leaks.
I see that in Memory Pool
in my MBeans
I can define UsageThreashold
for my Tenured Generation
.
So if my application exceeds this threashold the heap memory becomes red in the Memory tab.
Question: How does this help? I mean how am I supposed to use this setting to analyze my memory? How am I supposed to figure out this value?
Advertisement
Answer
In my opinion I don’t think that UsageThreashold
parameter is the most helpful for you to detect memory leaks (but if someone knows some tricks with it, please do share). In my experience that parameter is more helpful to visually understand if my application is getting way too near my max heap size and I’m in danger of getting an OutOfMemoryException
.
Still regarding using JConsole
to search for memory leaks, I don’t think there’s a silver bullet for the process. But what I usually do is the following:
If exists a memory leak, it means that the objects (the ones that are leaking) won’t get collected, hence, your Tenured Generation
won’t fully recover after any amount of GCs.
With the application running I connect JConsole
and try to spot a leak by observing the memory tab, if after several computations of my application and also after various GCs occurring (including pressing the Perform GC
button, which will result in a full gc) the memory never goes below, or at least to the memory value, it started tracking there’s a great possibility that something is leaking. When the leak is big, you can even see a “stair graph” pattern in your memory.
Keep in mind that if your application has long computations running, which may consume memory this analyzes must be done carefully. You must understand when those processes have finished. For example, just run one of those computations and track the total evolution of memory, before, during and afterwards.
Also, I suggest you to try visualVM instead, because it also allows you to create heap dumps, which you can use in order to understand which objects are still in memory and explore the references graph to understand why they are not being collected.