Over a million developers have joined DZone.

Memory Analysis: How to Obtain Java Heap Dump

DZone's Guide to

Memory Analysis: How to Obtain Java Heap Dump

Exploring automatic and manual Heap Dumps in Oracle JDK8 for troubleshooting memory issues.

Free Resource

SignalFx is the only real-time cloud monitoring platform for infrastructure, microservices, and applications. The platform collects metrics and traces across every component in your cloud environment, replacing traditional point tools with a single integrated solution that works across the stack.

In this article, we will see how to obtain the Java Heap Dump to troubleshoot memory issues like memory leaks and high usage of memory. There are different options available to obtain the Heap Dump. The options will vary based on the JVM vendors. For 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 the application throws an “OutOfMemoryError“. The JVM generates the Heap Dump file in the specified file path.

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

Sometimes we might require Heap Dump in an on-demand basis. For example, during the peak load, an application may become slower and the memory consumption might be more. To troubleshoot the memory consumption, we require Heap Dump. 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 something like java_pid.hprof.

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 the jvisualvm GUI tool to connect to local or remote JAVA processes. Through jvisualvm we can also 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 analyze the heap dumps to detect the memory issues. Until then, stay tuned!!! 

SignalFx is built on a massively scalable streaming architecture that applies advanced predictive analytics for real-time problem detection. With its NoSample™ distributed tracing capabilities, SignalFx reliably monitors all transactions across microservices, accurately identifying all anomalies. And through data-science-powered directed troubleshooting SignalFx guides the operator to find the root cause of issues in seconds.

memory leaks ,heap dump ,java

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}