{"id":1916,"date":"2013-02-27T15:43:55","date_gmt":"2013-02-27T07:43:55","guid":{"rendered":"http:\/\/rmohan.com\/?p=1916"},"modified":"2013-02-27T15:43:55","modified_gmt":"2013-02-27T07:43:55","slug":"java-heap-dump-and-analyze","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=1916","title":{"rendered":"Java Heap Dump and analyze"},"content":{"rendered":"<p>First get the Java Applications Process Id, one way to do this is with:\u00a0<em>jps<\/em><\/p>\n<p><em><code>jmap -dump:format=b,file=dump.bin &lt;javaProcessIdHere&gt;<\/code><\/em><\/p>\n<p>The size of the heap dump file will be same as the heap memory in use at the time the command is run. For large heap sizes this can take several minutes to run, and can stall (make it unresponsive) the application during this time.<\/p>\n<p>To get a summary of Object Instances and Sizes you can use this command:<\/p>\n<p><i>jmap -histo:live &lt;<em><code>javaProcessIdHere&gt;<\/code><\/em><br \/><\/i><\/p>\n<p>Generally, this command will run quickly, and should be used when taking a heap dump will be too costly in terms of time-taken\/size etc.<\/p>\n<p><strong>To get Heap Dump on Out of Memory Exception (available from Java 1.5.0_07 onwards):<\/strong><\/p>\n<p>Add the following param:\u00a0<em>-XX:+HeapDumpOnOutOfMemoryError<\/em><\/p>\n<p>If you want the dump to go in a particular path then add:\u00a0<em>-XX:+HeapDumpOnOutOfMemoryError\u00a0XX:HeapDumpPath=\/usr\/local\/dumps<\/em><\/p>\n<p><strong>To initiate Heap Dump from within the Application Code:<\/strong><\/p>\n<p>We can also use jmap from our code. To get a pid from code use we need to use java.lang.management.ManagementFactory.<br \/><code><br \/><em>String name = ManagementFactory.getRuntimeMXBean().getName();<\/em><br \/><em>String pid = name.substring(0, name.indexOf(\"@\"));<\/em><br \/><em>After that we can start jmap process like this:<\/em><br \/><em>String[] cmd = { \"jmap\", \"-dump:file=\/usr\/local\/heapdumps\/dump.bin\", pid };<\/em><br \/><em>Process p = Runtime.getRuntime().exec(cmd);<\/em><\/code><\/p>\n<p><strong>A simple Shell Script to Take Heap Dump every 30 seconds:<\/strong><\/p>\n<pre><em><code>while true\r\ndo\r\n  jmap -dump:file=\/tmp\/java-`date +%s`.hprof &lt;processIdOfJVM&gt;\r\n  sleep 30\r\ndone<\/code><\/em>\r\n\r\n<strong>Heap Dump Analysis Tools<\/strong><\/pre>\n<ul>\n<li>Java Visual VM (part of JDK)<\/li>\n<li>jhat \u2013 Java Heap Analysis Tool (Suitable for analyzing very big heap files)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>First get the Java Applications Process Id, one way to do this is with: jps<\/p>\n<p>jmap -dump:format=b,file=dump.bin &lt;javaProcessIdHere&gt;<\/p>\n<p>The size of the heap dump file will be same as the heap memory in use at the time the command is run. For large heap sizes this can take several minutes to run, and can stall [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1916"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1916"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1916\/revisions"}],"predecessor-version":[{"id":1918,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1916\/revisions\/1918"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}