<p>How do you find a memory leak in Java (using, for example, JHat)? I have tried to load the heap dump up in JHat to take a basic look. However, I do not understand how I am supposed to be able to find the root reference (<a href="https://stackoverflow.com/questions/104/anatomy-of-a-memory-leak">ref</a>) or whatever it is called. Basically, I can tell that there are several hundred megabytes of hash table entries ([java.util.HashMap$Entry or something like that), but maps are used all over the place... Is there some way to search for large maps, or perhaps find general roots of large object trees?</p>

<p>[Edit]
Ok, I've read the answers so far but let's just say I am a cheap bastard (meaning I am more interested in learning how to use JHat than to pay for JProfiler). Also, JHat is always available since it is part of the JDK. Unless of course there is no way with JHat but brute force, but I can't believe that can be the case.</p>

<p>Also, I do not think I will be able to actually modify (adding logging of <em>all</em> map sizes) and run it for long enough for me to notice the leak.</p>

[{'answer_id': 40124, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': "<p>Well, there's always the low tech solution of adding logging of the size of your maps when you modify them, then search the logs for which maps are growing beyond a reasonable size.</p>\n"}, {'answer_id': 40138, 'author': 'McKenzieG1', 'author_id': 3776, 'author_profile': 'https://Stackoverflow.com/users/3776', 'pm_score': 0, 'selected': False, 'text': '<p>You really need to use a memory profiler that tracks allocations. Take a look at <a href="http://www.ej-technologies.com/products/jprofiler/overview.html" rel="nofollow noreferrer">JProfiler</a> - their "heap walker" feature is great, and they have integration with all of the major Java IDEs. It\'s not free, but it isn\'t that expensive either ($499 for a single license) - you will burn $500 worth of time pretty quickly struggling to find a leak with less sophisticated tools.</p>\n'}, {'answer_id': 40146, 'author': 'Tnilsson', 'author_id': 4165, 'author_profile': 'https://Stackoverflow.com/users/4165', 'pm_score': 3, 'selected': False, 'text': '<p>There are tools that should help you find your leak, like JProbe, YourKit, AD4J or JRockit Mission Control. The last is the one that I personally know best. Any good tool should let you drill down to a level where you can easily identify what leaks, and where the leaking objects are allocated. </p>\n\n<p>Using HashTables, Hashmaps or similar is one of the few ways that you can acually leak memory in Java at all. If I had to find the leak by hand I would peridically print the size of my HashMaps, and from there find the one where I add items and forget to delete them.</p>\n'}, {'answer_id': 40214, 'author': 'wbkang', 'author_id': 2710, 'author_profile': 'https://Stackoverflow.com/users/2710', 'pm_score': 1, 'selected': False, 'text': '<p>NetBeans has a built-in profiler. </p>\n'}, {'answer_id': 40215, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 4, 'selected': False, 'text': '<p>A tool is a big help.</p>\n\n<p>However, there are times when you can\'t use a tool: the heap dump is so huge it crashes the tool, you are trying to troubleshoot a machine in some production environment to which you only have shell access, etc.</p>\n\n<p>In that case, it helps to know your way around the hprof dump file.</p>\n\n<p>Look for SITES BEGIN. This shows you what objects are using the most memory. But the objects aren\'t lumped together solely by type: each entry also includes a "trace" ID. You can then search for that "TRACE nnnn" to see the top few frames of the stack where the object was allocated. Often, once I see where the object is allocated, I find a bug and I\'m done. Also, note that you can control how many frames are recorded in the stack with the options to -Xrunhprof.</p>\n\n<p>If you check out the allocation site, and don\'t see anything wrong, you have to start backward chaining from some of those live objects to root objects, to find the unexpected reference chain. This is where a tool really helps, but you can do the same thing by hand (well, with grep). There is not just one root object (i.e., object not subject to garbage collection). Threads, classes, and stack frames act as root objects, and anything they reference strongly is not collectible.</p>\n\n<p>To do the chaining, look in the HEAP DUMP section for entries with the bad trace id. This will take you to an OBJ or ARR entry, which shows a unique object identifier in hexadecimal. Search for all occurrences of that id to find who\'s got a strong reference to the object. Follow each of those paths backward as they branch until you figure out where the leak is. See why a tool is so handy?</p>\n\n<p>Static members are a repeat offender for memory leaks. In fact, even without a tool, it\'d be worth spending a few minutes looking through your code for static Map members. Can a map grow large? Does anything ever clean up its entries?</p>\n'}, {'answer_id': 40285, 'author': 'Dima Malenko', 'author_id': 2586, 'author_profile': 'https://Stackoverflow.com/users/2586', 'pm_score': 8, 'selected': True, 'text': '<p>I use following approach to finding memory leaks in Java. I\'ve used jProfiler with great success, but I believe that any specialized tool with graphing capabilities (diffs are easier to analyze in graphical form) will work.</p>\n\n<ol>\n<li>Start the application and wait until it get to "stable" state, when all the initialization is complete and the application is idle.</li>\n<li>Run the operation suspected of producing a memory leak several times to allow any cache, DB-related initialization to take place.</li>\n<li>Run GC and take memory snapshot.</li>\n<li>Run the operation again. Depending on the complexity of operation and sizes of data that is processed operation may need to be run several to many times.</li>\n<li>Run GC and take memory snapshot.</li>\n<li>Run a diff for 2 snapshots and analyze it.</li>\n</ol>\n\n<p>Basically analysis should start from greatest positive diff by, say, object types and find what causes those extra objects to stick in memory.</p>\n\n<p>For web applications that process requests in several threads analysis gets more complicated, but nevertheless general approach still applies.</p>\n\n<p>I did quite a number of projects specifically aimed at reducing memory footprint of the applications and this general approach with some application specific tweaks and trick always worked well.</p>\n'}, {'answer_id': 56031, 'author': 'jwiklund', 'author_id': 4208, 'author_profile': 'https://Stackoverflow.com/users/4208', 'pm_score': 6, 'selected': False, 'text': '<p>Questioner here, I have got to say getting a tool that does not take 5 minutes to answer any click makes it a lot easier to find potential memory leaks.</p>\n\n<p>Since people are suggesting several tools ( I only tried visual wm since I got that in the JDK and JProbe trial ) I though I should suggest a free / open source tool built on the Eclipse platform, the Memory Analyzer (sometimes referenced as the SAP memory analyzer) available on <a href="http://www.eclipse.org/mat/" rel="noreferrer">http://www.eclipse.org/mat/</a> . </p>\n\n<p>What is really cool about this tool is that it indexed the heap dump when I first opened it which allowed it to show data like retained heap without waiting 5 minutes for each object (pretty much all operations were tons faster than the other tools I tried).</p>\n\n<p>When you open the dump, the first screen shows you a pie chart with the biggest objects (counting retained heap) and one can quickly navigate down to the objects that are to big for comfort. It also has a Find likely leak suspects which I reccon can come in handy, but since the navigation was enough for me I did not really get into it.</p>\n'}, {'answer_id': 31018959, 'author': 'Alex Punnen', 'author_id': 429476, 'author_profile': 'https://Stackoverflow.com/users/429476', 'pm_score': 4, 'selected': False, 'text': '<p>Most of the time, in enterprise applications the Java heap given is larger than the ideal size of max 12 to 16 GB. I have found it hard to make the NetBeans profiler work directly on these big java apps.</p>\n\n<p>But usually this is not needed. You can use the jmap utility that comes with the jdk to take a "live" heap dump , that is jmap will dump the heap after running GC.  Do some operation on the application, wait till the operation is completed, then take another "live" heap dump. Use tools like Eclipse MAT to load the heapdumps, sort on the histogram, see which objects have increased, or which are the highest, This would give a clue.</p>\n\n<pre><code>su  proceeuser\n/bin/jmap -dump:live,format=b,file=/tmp/2930javaheap.hrpof 2930(pid of process)\n</code></pre>\n\n<p>There is only one problem with this approach; Huge heap dumps, even with the live option, may be too big to transfer out to development lap, and may need a machine with enough memory/RAM to open.</p>\n\n<p>That is where the class histogram comes into picture. You can dump a live class histogram with the jmap tool. This will give only the class histogram of memory usage.Basically it won\'t have the information to chain the reference. For example it may put char array at the top. And String class somewhere below. You have to draw the connection yourself. </p>\n\n<pre><code>jdk/jdk1.6.0_38/bin/jmap -histo:live 60030 &gt; /tmp/60030istolive1330.txt\n</code></pre>\n\n<p>Instead of taking two heap dumps, take two class histograms, like as described above; Then compare the class histograms and see the classes that are increasing. See if you can relate the Java classes to your application classes. This will give a pretty good hint. Here is a pythons script that can help you compare two  jmap histogram dumps. <a href="https://gist.github.com/alexcpn/a68761c94c85f0210413" rel="noreferrer">histogramparser.py</a></p>\n\n<p>Finally tools like JConolse and VisualVm are essential to see the memory growth over time, and see if there is a memory leak. Finally sometimes your problem may not be a memory leak , but high memory usage.For this enable GC logging;use a more advanced and new compacting GC like G1GC; and you can use jdk tools like jstat to see the GC behaviour live</p>\n\n<pre><code>jstat -gccause pid &lt;optional time interval&gt;\n</code></pre>\n\n<p>Other referecences to google for -jhat, jmap, Full GC, Humongous allocation, G1GC</p>\n'}, {'answer_id': 57993151, 'author': 'Amir Fo', 'author_id': 7580839, 'author_profile': 'https://Stackoverflow.com/users/7580839', 'pm_score': 0, 'selected': False, 'text': '<p>You can find out by measuring memory usage size after calling garbage collector multiple times:</p>\n\n<pre><code>Runtime runtime = Runtime.getRuntime();\n\nwhile(true) {\n    ...\n    if(System.currentTimeMillis() % 4000 == 0){\n        System.gc();\n        float usage = (float) (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024;\n        System.out.println("Used memory: " + usage + "Mb");\n    }\n\n}\n</code></pre>\n\n<p>If the output numbers were equal, there is no memory leak in your application, but if you saw difference between the numbers of memory usage (increasing numbers), there is memory leak in your project. For example:</p>\n\n<pre><code>Used memory: 14.603279Mb\nUsed memory: 14.737213Mb\nUsed memory: 14.772224Mb\nUsed memory: 14.802681Mb\nUsed memory: 14.840599Mb\nUsed memory: 14.900841Mb\nUsed memory: 14.942261Mb\nUsed memory: 14.976143Mb\n</code></pre>\n\n<p><strong>Note</strong> that sometimes it takes some time to release memory by some actions like streams and sockets. You should not judge by first outputs, You should test it in a specific amount of time.</p>\n'}, {'answer_id': 58967398, 'author': 'Vaibhav Jain', 'author_id': 3128652, 'author_profile': 'https://Stackoverflow.com/users/3128652', 'pm_score': 0, 'selected': False, 'text': '<p>Checkout this <a href="https://www.ej-technologies.com/blog/2017/03/finding-a-memory-leak-with-jprofiler/" rel="nofollow noreferrer">screen cast</a> about finding memory leaks with JProfiler.\nIt\'s visual explanation of @Dima Malenko Answer.</p>\n\n<p>Note: Though JProfiler is not freeware, But Trial version can deal with current situation.</p>\n'}, {'answer_id': 59315884, 'author': 'Sreeram Nair', 'author_id': 1853243, 'author_profile': 'https://Stackoverflow.com/users/1853243', 'pm_score': 0, 'selected': False, 'text': '<p>As most of us use Eclipse already for writing code, Why not use the Memory Analyser Tool(MAT) in Eclipse. It works great.</p>\n\n<p>The <strong>Eclipse MAT</strong> is a set of plug-ins for the Eclipse IDE which provides tools to analyze <code>heap dumps</code> from Java application and to identify <code>memory problems</code> in the application. </p>\n\n<p><strong>This helps the developer to find memory leaks with the following features</strong></p>\n\n<ol>\n<li>Acquiring a memory snapshot (Heap Dump)</li>\n<li>Histogram</li>\n<li>Retained Heap</li>\n<li>Dominator Tree</li>\n<li>Exploring Paths to the GC Roots</li>\n<li>Inspector</li>\n<li>Common Memory Anti-Patterns</li>\n<li>Object Query Language</li>\n</ol>\n\n<p><a href="https://i.stack.imgur.com/kznIo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kznIo.png" alt="enter image description here"></a></p>\n'}, {'answer_id': 66785045, 'author': 'Jafar Karuthedath', 'author_id': 2668564, 'author_profile': 'https://Stackoverflow.com/users/2668564', 'pm_score': 0, 'selected': False, 'text': '<p>I have recently dealt with a memory leak in our application. Sharing my experience here</p>\n<p>The garbage collector removes unreferenced objects periodically, but it never collects the objects that are still being referenced. This is where memory leaks can occur.</p>\n<p>Here is some options to find out the referenced objects.</p>\n<ol>\n<li>Using <code>jvisualvm</code> which is located in <code>JDK/bin</code> folder <br></li>\n</ol>\n<p>Options : Watch Heap space\n<a href="https://i.stack.imgur.com/QGyFD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QGyFD.png" alt="enter image description here" /></a></p>\n<p>If you see that the heap space keep increasing, definitely there is a memory leak.</p>\n<p>To find out the cause, you can use <code>memory sampler</code> under <code>sampler</code> .</p>\n<p><a href="https://i.stack.imgur.com/ArgS1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ArgS1.png" alt="enter image description here" /></a></p>\n<ol start="2">\n<li><p>Get a Java heap histogram by using <code>jmap</code> ( which is also available in <code>JDK/bin</code> folder) in different time span of the application</p>\n<pre><code>jmap -histo &lt;pid&gt; &gt; histo1.txt\n</code></pre>\n</li>\n</ol>\n<p>Here the object reference can be analyzed. If some of the objects are never garbage collected, that is the potential memory leak.</p>\n<p>You can read the some of the most common cause of memory leak here in this article : <a href="https://www.baeldung.com/java-memory-leaks" rel="nofollow noreferrer">Understanding Memory Leaks in Java</a></p>\n'}]
['https://Stackoverflow.com/questions/40119', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4208/']