Search code examples
javamemory-leaksjprofiler

How can I find source of java memory leak with JProfiler


I have a memory leak in my program that I've been analyzing with JProfiler. There are some string values, which I recognize as originating from my program, that are not getting garbage collected. However, when looking at the Heap Walker in JProfiler I'm not quite sure how to narrow down the source.

JProfiler Screenshot

The string is data coming from some HTML parsing I do, however I'm not certain how there is still a reference to the object. All I see is "thread object". How can I find out where the actual memory leak is? Is that possible?


Solution

  • Just looking at the garbage collector root will not always tell you what causes the memory leak, you have to analyze the while reference chain.

    The screen shot shows a java.util.Stack object in the reference chain that you have put into a java.lang.ThreadLocal. Thread locals are a common source of memory leaks.

    To see which thread local references your objects, select the object(s) of interest in a new object set, then calculate the "Thread locals" inspection in the heap walker.

    enter image description here