Differences Between Heap Dump, Thread Dump and Core Dump – 堆转储、线程转储和核心转储之间的区别

最后修改: 2023年 9月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

A dump is data queried from a storage medium and stored somewhere for further analysis. The Java Virtual Machine (JVM) helps to manage memory in Java, and in the case of errors, we can get a dump file from the JVM to diagnose errors.

转储是从存储介质中查询的数据,并存储在某处以备进一步分析Java虚拟机(JVM)有助于管理Java中的内存,在出现错误时,我们可以从JVM中获取转储文件来诊断错误。

In this tutorial, we’ll explore three common Java dump files – heap dump, thread dump, and core dump – and understand their use cases.

在本教程中,我们将探讨三种常见的 Java dump 文件(堆转储、线程转储和内核转储),并了解它们的使用情况。

2. Heap Dump

2.堆转储

During runtime, the JVM creates the heap, which contains references to objects in use in a running Java application. The heap dump contains a saved copy of the current state of all objects in use at runtime.

在运行期间,JVM 会创建堆,其中包含对运行中 Java 应用程序中使用的对象的引用。堆转储包含运行时使用的所有对象的当前状态的保存副本

Additionally, it’s used to analyze the OutOfMemoryError errors in Java.

此外,它还用于分析 Java 中的 OutOfMemoryError 错误

Furthermore, the heap dump can be in two formats – the classic format and the Portable Heap Format (PHD).

此外,堆转储有两种格式–经典格式和可移植堆格式 (PHD)

The classic format is human-readable, while the PHD is in binary and needs tools for further analysis. Also, PHD is the default for a heap dump.

经典格式可由人工读取,而 PHD 是二进制格式,需要使用工具进行进一步分析。此外,PHD 是堆转储的默认格式。

Moreover, modern heap dumps also contain some thread information. Starting from Java 6 update 14, a heap dump also contains stack traces for threads. The stack traces in the heap dump connect objects to the threads using them.

此外,现代堆转储还包含一些线程信息。从 Java 6 更新 14 开始,堆转储还包含线程的堆栈跟踪。堆转储中的堆跟踪将对象与使用这些对象的线程连接起来

Analysis tools like Eclipse Memory Analyzer include support to retrieve this information.

Eclipse Memory Analyzer 等分析工具支持检索这些信息。

2.1. Use Case

2.1.用例

Heap dumps can help when analyzing OutOfMemoryError in a Java application.

堆转储有助于分析 Java 应用程序中的 OutOfMemoryError

Let’s see some example code that throws OutOfMemoryError:

让我们来看一些抛出 OutOfMemoryError 的示例代码:

public class HeapDump {
    public static void main(String[] args) {
        List numbers = new ArrayList<>();
        try {
            while (true) {
                numbers.add(10);
            }
        } catch (OutOfMemoryError e) {
            System.out.println("Out of memory error occurred!");
        }
    }
}

In the example code above, we create a scenario of an infinite loop until the heap memory is full. As we know, the new keyword helps to allocate memory on the heap in Java.

在上面的示例代码中,我们创建了一个无限循环的场景,直到堆内存已满。我们知道,在 Java 中,new 关键字有助于在堆上分配内存。

To capture the heap dump of the code above, we’ll need a tool. One of the most used tools is jmap.

要捕获上述代码的堆转储,我们需要一个工具。最常用的工具之一是jmap

First, we need to get the process ID of all running Java processes on our machine by running the jps command:

首先,我们需要运行 jps 命令,获取机器上所有正在运行的 Java 进程的进程 ID:

$ jps

The command above outputs to the console all running Java processes:

上述命令将向控制台输出所有正在运行的 Java 进程:

12789 Launcher
13302 Jps
7517 HeapDump

Here, our process of interest is HeapDump. Therefore, let’s run the jmap command with the HeapDump process ID to capture the heap dump:

在这里,我们感兴趣的进程是 HeapDump。因此,让我们使用 HeapDump 进程 ID 运行 jmap 命令来捕获堆转储:

 $ jmap -dump:live,file=hdump.hprof 7517

The command above generates the hdump.hprof file in the project root directory.

上述命令将在项目根目录下生成 hdump.hprof 文件。

Finally, we can use tools like Eclipse Memory Analyzer to analyze the dump file.

最后,我们可以使用 Eclipse Memory Analyzer 等工具来分析转储文件

3. Thread Dump

3.线程转储

The thread dump contains the snapshot of all threads in a running Java program at a specific instant.

线程转储包含正在运行的 Java 程序中所有线程在特定时刻的快照

A thread is the smallest part of a process that helps a program to operate efficiently by running multiple tasks concurrently.

线程是进程的最小部分,它通过同时运行多个任务来帮助程序高效运行。

Furthermore, a thread dump can help diagnose efficiency issues in a Java application. Thus, it’s a vital tool for analyzing performance issues, especially when an application is slow.

此外,线程转储可以帮助诊断 Java 应用程序中的效率问题。因此,它是分析性能问题的重要工具,尤其是当应用程序运行缓慢时。

Additionally, it can help detect threads stuck in an infinite loop. It can also help identify deadlocks, where multiple threads are waiting for one other to release resources.

此外,它还可以帮助检测陷入无限循环的线程它还可以帮助识别死锁,即多个线程正在等待彼此释放资源

Additionally, it can identify a situation where certain threads aren’t getting enough CPU time. This can help identify performance bottlenecks.

此外,它还能识别某些线程没有获得足够 CPU 时间的情况。这有助于识别性能瓶颈。

3.1. Use Case

3.1.用例

Here’s an example program that can potentially have a slow performance due to a long-running task:

下面是一个示例程序,它可能会因为长时间运行任务而导致性能缓慢:

public class ThreadDump {
    public static void main(String[] args) {
        longRunningTask();
    }
    
    private static void longRunningTask() {
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            if (Thread.currentThread().isInterrupted()) {
                System.out.println("Interrupted!");
                break;
            }
            System.out.println(i);
        }
    }
}

In the sample code above, we create a method that loops through to Integer.MAX_VALUE and outputs the value to the console. This is a long-running operation and will potentially be a performance issue.

在上面的示例代码中,我们创建了一个方法,该方法循环到 Integer.MAX_VALUE 并将值输出到控制台。这是一个长期运行的操作,可能会产生性能问题

To analyze the performance, we can capture the thread dump. First, let’s find the process ID of all running Java programs:

要分析性能,我们可以捕获线程转储。首先,让我们查找所有正在运行的 Java 程序的进程 ID:

$ jps

The jps command outputs all Java processes to the console:

jps 命令将所有 Java 进程输出到控制台:

3042 ThreadDump
964 Main
3032 Launcher
3119 Jps

We have an interest in the ThreadDump process ID. Next, let’s use the jstack command with the process ID to take the thread dump:

我们对ThreadDump进程 ID 很感兴趣。接下来,让我们使用带有进程 ID 的 jstack 命令来进行线程转储

$ jstack -l 3042 > slow-running-task-thread-dump.txt

The command above captures the thread dump and saves it in a txt file for further analysis.

上述命令捕获线程转储并将其保存在 txt 文件中,以便进一步 分析

4. Core Dump

4.核心转储

The core dump, also known as the crash dump, contains the snapshot of a program when the program crashed or abruptly terminated.

核心转储也称为崩溃转储,包含程序崩溃或突然终止时的快照

The JVM runs bytecode and not native code. Hence, Java code cannot cause core dumps.

JVM 运行字节码而非本地代码。因此,Java 代码不会导致核心转储。

However, some Java programs use Java Native Interface (JNI) to run native code directly. It’s possible for the JNI to crash the JVM because external libraries can crash. We can take the core dump at that instant and analyze it.

但是,有些 Java 程序使用 Java 本地接口 (JNI) 直接运行本地代码。JNI 有可能导致 JVM 崩溃,因为外部库可能会崩溃。我们可以提取当时的核心转储并进行分析。

Furthermore, a core dump is an OS-level dump and can be used to find the details of native calls when a JVM crashes.

此外, 内核转储是一种操作系统级转储,可用于在 JVM 崩溃时查找本地调用的详细信息

4.1. Use Case

4.1.用例

Let’s see an example that generates a core dump using JNI.

让我们来看一个使用 JNI 生成核心转储的示例。

First, let’s create a class named CoreDump to load a native library:

首先,让我们创建一个名为CoreDump的类来加载本地库:

public class CoreDump {
    private native void core();
    public static void main(String[] args) {
        new CoreDump().core();
    }
    static {
        System.loadLibrary("nativelib");
    }
}

Next, let’s compile the Java code using the javac command:

接下来,让我们使用 javac 命令编译 Java 代码:

$ CoreDump.java

Then, let’s generate a header for native method implementation by running the javac -h command:

然后,让我们运行 javac -h 命令,为本地方法的实现生成头文件:

$ javac -h . CoreDump.java

Finally, let’s implement a native method in C that will crash the JVM:

最后,让我们用 C 语言实现一个会让 JVM 崩溃的本地方法:

#include <jni.h>
#include "CoreDump.h"
    
void core() {
    int *p = NULL;
    *p = 0;
}
JNIEXPORT void JNICALL Java_CoreDump_core (JNIEnv *env, jobject obj) {
    core();
};
void main() {
}

Let’s compile the native code by running the gcc command:

让我们运行 gcc 命令来编译本地代码:

$ gcc -fPIC -I"/usr/lib/jvm/java-17-graalvm/include" -I"/usr/lib/jvm/java-17-graalvm/include/linux" -shared -o libnativelib.so CoreDump.c

This generates shared libraries named libnativelib.so. Next, let’s compile the Java code with the shared libraries:

这将生成名为 libnativelib.so 的共享库。接下来,让我们用共享库编译 Java 代码:

$ java -Djava.library.path=. CoreDump

The native method crashed the JVM and generated a core dump in the project directory:

本地方法导致 JVM 崩溃,并在项目目录中生成了核心转储:

// ...
# A fatal error has been detected by the Java Runtime Environment:
# SIGSEGV (0xb) at pc=0x00007f9c48878119, pid=65743, tid=65744
# C  [libnativelib.so+0x1119]  core+0x10
# Core dump will be written. Default location: Core dumps may be processed with 
# "/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h" (or dumping to /core-java-perf/core.65743)
# An error report file with more information is saved as:
# ~/core-java-perf/hs_err_pid65743.log
// ...

The above output shows the crash information and the location of the dump file.

上述输出显示了崩溃信息和转储文件的位置。

5. Key Differences

5.主要差异

Here’s a summary table showing the key differences between three types of Java dump files:

下面的汇总表显示了三种 Java 转储文件的主要区别:

Dump Type Use Case Contains
Heap Dump Diagnose memory issues like OutOfMemoryError Snapshot of objects in the Java heap
Thread Dump Troubleshoot performance issues, thread deadlocks, and infinite loops Snapshot of all thread states in the JVM
Core Dump Debug crashes caused by native libraries Process state when JVM crashes

6. Conclusion

6.结论

In this article, we learned the differences between heap dump, thread dump, and core dump by looking at their uses. Additionally, we saw example code with different issues and generated a dump file for further analysis. Each dump file serves a different purpose for troubleshooting Java applications.

在本文中,我们通过查看堆转储、线程转储和内核转储的用途,了解了它们之间的区别。此外,我们还看到了存在不同问题的示例代码,并生成了一个转储文件以作进一步分析。每个转储文件在 Java 应用程序故障诊断中都有不同的用途。

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

与往常一样,这些示例的源代码可在 GitHub 上获取。