1. 概述

本文我们将介绍捕获 Java Heap Dumps(堆转储)的几种方法。

Heap Dump 中文翻译为堆转储,是某个时刻 JVM 内存中所有对象的快照。它们对于解决Java程序中的内存泄漏问题和优化内存使用非常有用。

Heap Dump 通常储存在二进制格式的hprof文件中。 我们可以使用 jhat 或 JVisualVM 之类的工具打开和分析这些文件。 对于Eclipse用户,使用 MAT 的也比较多。

下面,我们将介绍几种工具和方法来生成Heap Dump,并说明它们之间的主要区别。

2. JDK 工具

JDK自带了几种工具可以用来捕获heap dump,这些工具存放在JDK目录下的bin文件夹中。只要此路径在系统环境变量中,我们就可以直接在命令行中运行它们。

2.1. jmap

jmap 是一个用于打印运行中的 JVM 内存统计信息的工具。目标进程可以是本地或远程进程。

要使用 jmap 捕获 heap dump,我们需要添加 dump 选项

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

dump选项有几个参数:

  • live : if set it only prints objects which have active references and discards the ones that are ready to be garbage collected. This parameter is optional
  • format=b : specifies that the dump file will be in binary format. If not set the result is the same
  • file : the file where the dump will be written to
  • pid : id of the Java process

用例:

jmap -dump:live,format=b,file=/tmp/dump.hprof 12587

温馨提示,我们可以使用 jps命令很方便的获取 Java 进程的pid。

注意,jmap 是 JDK 中的一个实验性工具,已经不再受支持。 所以,最好选择使用其他工具代替。

2.2. jcmd

jcmd 是一个非常完整的工具,用于向JVM发送命令。使用此工具需要与运行的Java程序在同一台机器上。

我们可以使用 _GC.heap_dump_命令 来获取heap dump,需要指定进程的pid和 heap dump 文件输出路径。

jcmd <pid> GC.heap_dump <file-path>

用例:

jcmd 12587 GC.heap_dump /tmp/dump.hprof

和 jmap 一样,生成的 dump 文件是二进制格式的。

2.3. JVisualVM

JVisualVM 是一个用于监控、故障排查和分析Java应用程序的图形化界面工具。界面很简洁,但使用起来非常直观和简单。

在左侧 Java进程列表中,我们鼠标右键选择 “堆 Dump(H)” 选项 ,该工具将生成一个heap dump,并在右侧新建的tab窗口中打开。

生成的dump文件路径可以在 “基本信息” 中看到。

从 JDK 9 开始,Visual VM 不再包括在 Oracle JDK 和 Open JDK 发行版中。 因此,如果使用的是Java 9 或更高版本,则需要从 Visual VM 站点下载 JVisualVM。

3. 自动捕捉 Heap Dump

All the tools that we've shown in the previous sections are intended to capture heap dumps manually at a specific time. In some cases, we want to get a heap dump when a java.lang.OutOfMemoryError occurs so it helps us investigate the error.

For these cases, Java provides the HeapDumpOnOutOfMemoryError command-line option that generates a heap dump when a java.lang.OutOfMemoryError is thrown:

java -XX:+HeapDumpOnOutOfMemoryError

By default, it stores the dump in a java_pid<pid>.hprof file in the directory where we're running the application. If we want to specify another file or directory we can set it in the HeapDumpPath option:

java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<file-or-dir-path>

When our application runs out of memory using this option, we'll be able to see in the logs the created file that contains the heap dump:

java.lang.OutOfMemoryError: Requested array size exceeds VM limit
Dumping heap to java_pid12587.hprof ...
Exception in thread "main" Heap dump file created [4744371 bytes in 0.029 secs]
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
    at com.baeldung.heapdump.App.main(App.java:7)

In the example above, it was written to the java_pid12587.hprof file.

As we can see, this option is very useful and there is no overhead when running an application with this option. Therefore, it's highly recommended to use this option always, especially in production.

Finally, this option can also be specified at runtime by using the HotSpotDiagnostic MBean. To do so, we can use JConsole and set the HeapDumpOnOutOfMemoryError VM option to true:

We can find more information about MBeans and JMX in this article.

4. JMX

The last approach that we'll cover in this article is using JMX. We'll use the HotSpotDiagnostic MBean that we briefly introduced in the previous section. This MBean provides a dumpHeap method that accepts 2 parameters:

  • outputFile: the path of the file for the dump. The file should have the hprof extension
  • live: if set to true it dumps only the active objects in memory, as we've seen with jmap before

In the next sections, we'll show 2 different ways to invoke this method in order to capture a heap dump.

4.1. JConsole

The easiest way to use the HotSpotDiagnostic MBean is by using a JMX client such as JConsole.

If we open JConsole and connect to a running Java process, we can navigate to the MBeans tab and find the HotSpotDiagnostic under com.sun.management. In operations, we can find the dumpHeap method that we've described before:

As shown, we just need to introduce the parameters outputFile and live into the p0 and p1 text fields in order to perform the dumpHeap operation.

4.2. 编程的方式

The other way to use the HotSpotDiagnostic MBean is by invoking it programmatically from Java code.

To do so, we first need to get an MBeanServer instance in order to get an MBean that is registered in the application. After that, we simply need to get an instance of a HotSpotDiagnosticMXBean and call its dumpHeap method.

Let's see it in code:

public static void dumpHeap(String filePath, boolean live) throws IOException {
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(
      server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
    mxBean.dumpHeap(filePath, live);
}

Notice that an hprof file cannot be overwritten. Therefore, we should take this into account when creating an application that prints heap dumps. If we fail to do so we'll get an exception:

Exception in thread "main" java.io.IOException: File exists
    at sun.management.HotSpotDiagnostic.dumpHeap0(Native Method)
    at sun.management.HotSpotDiagnostic.dumpHeap(HotSpotDiagnostic.java:60)

5. 总结

In this tutorial, we've shown multiple ways to capture a heap dump in Java.

As a rule of thumb, we should remember to use the HeapDumpOnOutOfMemoryError option always when running Java applications. For other purposes, any of the other tools can be perfectly used as long as we keep in mind the unsupported status of jmap.

As always, the full source code of the examples is available over on GitHub.