Different Ways to Capture Java Heap Dumps


A heap dump is a snapshot of the memory more specifically the Java heap memory of a Java process at a certain point in time. It contains information about the Java objects and classes. Heap dumps are useful when we need to debug memory problems such as memory leaks, high memory consumption, and out of memory errors. Java provides several ways to capture these heap dumps that we are going to explore in this article.

Ways to Capture Java Heap Dumps

The following ways can be used to capture Java heap dumps:

  • HeapDumpOnOutOfMemoryError

  • Jcmd

  • jmap

Let's discuss these approaches sequentially.

Using HeapDumpOnOutOfMemoryError

There are a few ways that capture heap dumps manually which we will discuss later. This section will provide us with an overview of HeapDumpOnOutOfMemoryError which allows us to capture heap dumps automatically.

In Java, 'OutOfMemoryError' occurs when heap memory ran out of space. This is the most preferable time to capture heap dump because it is easy to investigate and debug memory errors at the time of occurrence.

We can configure the HeapDumpOnOutOfMemoryError, which generates a heap dump when a Java application throws java.lang.OutOfMemoryError.

In this case, we need to pass the following system property:

java -XX:+HeapDumpOnOutOfMemoryError

The default directory where heap dumps get stored is the java_pid.hprof file. We can get this file where we are running the application.

Using jcmd

The 'jcmd' is a command-line tool that is part of the JDK. It can be used to send diagnostic commands to a running Java process. One of these commands is GC.heap_dump, which is used to generate a heap dump from a specified Java process.

To use jcmd, we need to know the process ID for our convenience, we can call it PID of the Java application. One can find the PID using the command 'jps'.

Use the following command to generate a heap dump:

jcmd <pid> GC.heap_dump <filename>

Using jmap

It is another command-line tool that is part of the JDK. It can be used to print heap information and generate heap dumps from a running Java process or a core file. Similar to jcmd, we need to know the process ID in short we can call it PID of the Java application to use jmap. One can find the PID using the command 'jps'.

To generate a heap dump, use the following command:

jmap -dump:[live], format = b, file = <pathOffile> <pid>

Here,

live: It is an optional yet important parameter as it dumps only the live objects in the heap. In other words, if we pass this option it writes only that objects which have active references.

format: It indicates the format of heap dump files. If it is set to 'b' means it will have binary format.

pathOffile: It is used to specify the file path where heap dumps will get written.

pid: It specifies the process ID.

This will create a binary heap dump file with the specified name. As a reminder, note that sometimes this tool won't get us desired result as it is deprecated by Java. It is better to use other tools discussed in this article.

Conclusion

We started this article by defining the Java Heap Dumps and in the next section, we proposed a few approaches for capturing heap dumps. We discussed three ways of doing so, the HeapDumpOnOutOfMemoryError is a way to capture heap dump automatically and jmap and jcmd command line tools are used for manual capturing.

Updated on: 19-Jul-2023

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements