Memory Analysis – How to obtain Java Heap Dump?

Java Heap Dump

In this article we will see how to obtain the Java Heap Dump to troubleshoot the memory issues like memory leak and high usage of memory. There are different options available to obtain the Heap Dump. The options will vary based on the JVM vendors. In this article I used Oracle JDK8.

Automatically generate the Heap Dump when application throws “OutOfMemoryError”

In this approach, we have to pass the below JVM arguments to get the Heap Dump when application throws “OutOfMemoryError“. The JVM generates the Heap Dump file in the specified file path.

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<path to this heap dump file>

If you have not specified the HeapDumpPath, then the JVM generates the file where the JAVA process is running. The name of the dump file will be like java_pid.hprof.

Some times we might require Heap Dump on demand basis. For example, during the peak load application may become slower and the memory consumption might be more. To troubleshoot the memory consumption, we require Heap Dump.

Manually Generating a Heap Dump

From JDK6, we are having jmap tool to generate the Heap Dump on demand basis. jmap will dump the Java heap in binary HPROF format to a specified file. The instruction is given below.

jmap -dump:format=b,file=<file_name> <pid>
For example, jmap -dump:format=b,file=heap_dump.hprof 4988

In the above example, the 4988 is the java process id to get the heap dump. We can obtain the process id by using jps(Java Virtual Machine Process Status Tool)

There is another GUI utility called jconsole. jconsole can connect to local java process or remote java process. By using MBeans we can get the heap dump.

We can use jvisualvm GUI tool to connect to local or remote JAVA processes. Through jvisualvm also we can generate the heap dump.

All the above said tools are part of JDK. Set the JAVA_HOME and PATH environment variables to access the tools.

In the next article we will see the tools to analyse the heap dumps to detect the memory issues. Till then, stay tune!!! 

 

Advertisements

I am Siva Prasad Rao Janapati. Working as a software developer. Has hands on experience on ATG Commerce(DAS/DPS/DCS), Mozu commerce, Broadleaf Commerce, Java, JEE, Spring, Play, JPA, Hibernate, Velocity, JMS, Jboss, Weblogic,Tomcat, Jetty, Apache, Apache Solr, Spring Batch, JQuery, NodeJS, SOAP, REST, MySQL, Oracle, Mongo DB, Memcached, HazelCast, Git, SVN, CVS, Ant, Maven, Gradle, Amazon Web services, Rackspace, Quartz, JMeter, Junit, Open NLP, Facebook Graph,Twitter4J, YouTube Gdata, Bazzarvoice,Yotpo, 4-Tell, Alatest, Shopzilla, Linkshare. I have hands on experience on open sources and commercial technologies.

Tagged with: , , ,
Posted in Java, Performance
One comment on “Memory Analysis – How to obtain Java Heap Dump?
  1. Prasad says:

    Good content

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

DZone

DZone MVB

Java Code Geeks
Java Code Geeks
%d bloggers like this: