杰瑞科技汇

Linux下如何dump Java进程内存快照?

Of course. The phrase "Linux dump java" can mean several different things depending on the context. Here’s a comprehensive breakdown of the most common interpretations, from creating a core dump of a Java process to analyzing it and other "dump"-related concepts.

Linux下如何dump Java进程内存快照?-图1
(图片来源网络,侵删)

Summary of Meanings

Scenario What You're Doing Primary Tools
Creating a Core Dump Saving the memory state of a crashed or hanging Java process to a file for later analysis. gcore, kill -QUIT, ulimit, jcmd
Analyzing a Core Dump Examining the core dump file to find the cause of a crash (e.g., OutOfMemoryError, native crash). jhat, jstack, VisualVM, Eclipse MAT
"Heap Dump" (Java-specific) Saving a snapshot of the Java object heap to analyze memory usage, find memory leaks. jmap, jcmd GC.heap_info, VisualVM
"Thread Dump" (Java-specific) Saving a snapshot of all active threads to analyze deadlocks, performance bottlenecks. jstack, jcmd Thread.print, kill -3

Scenario 1: Creating a Core Dump of a Java Process

A core dump is a snapshot of the memory (RAM) of a running process at the moment it crashed or was explicitly told to dump its state. This is the most literal interpretation of "dump java".

Method A: Using gcore (Recommended)

gcore is part of GDB (GNU Debugger) and is the cleanest way to create a core dump of a live process without stopping it.

  1. Find the Java Process ID (PID):

    # List all java processes
    ps -ef | grep java
    # Or use pgrep for a more direct approach
    pgrep java

    Let's say the PID is 12345.

    Linux下如何dump Java进程内存快照?-图2
    (图片来源网络,侵删)
  2. Create the Core Dump:

    # The syntax is gcore <pid> <output_filename>
    gcore 12345 java_core_12345.dump

    This will create a file named java_core_12345.dump in your current directory. The Java process will continue running normally.

  3. (Optional) Ensure Core Dumps are Enabled: Sometimes, core dumps are disabled by system limits. You can check and set the limit for the current user:

    # Check the current limit (often 0, which means disabled)
    ulimit -c
    # Set the core dump file size limit to unlimited (or a large number like 'unlimited')
    ulimit -c unlimited
    # Set the location where core dumps will be saved
    echo "/tmp/core-dumps/core.%e.%p" | sudo tee /proc/sys/kernel/core_pattern
    • %e is replaced by the executable name.
    • %p is replaced by the PID.

Method B: Using kill -QUIT (For Crashes)

This signal tells the JVM to perform a "safe" dump and continue running. It's less disruptive than a crash but is primarily used to generate a thread dump or heap dump on some JVMs (like older Oracle/OpenJDK). For a true core dump, gcore is better.

Linux下如何dump Java进程内存快照?-图3
(图片来源网络,侵删)
# Syntax: kill -QUIT <pid>
kill -QUIT 12345

The JVM will print the dump information to its stdout/stderr (usually the console or logs).


Scenario 2: Analyzing a Core Dump

Once you have a .dump file, you need to analyze it. The standard tool for this is jhat, but more modern and user-friendly tools are highly recommended.

Method A: Using jhat (JVM Heap Analysis Tool)

jhat is a basic tool included with the JDK. It starts a web server to browse the heap.

# Syntax: jhat <core_dump_file>
jhat java_core_12345.dump

This will start a server, usually on http://localhost:7000. You can then open this URL in a browser to inspect objects, find memory leaks, etc. Note: jhat is very slow and has been deprecated in recent JDK versions. It's better to use the tools below.

Method B: Using Eclipse Memory Analyzer Tool (MAT) (Highly Recommended)

MAT is a powerful, standalone GUI tool specifically designed for analyzing Java heap dumps and core dumps.

  1. Download and install MAT from the Eclipse MAT website.
  2. Open the core dump file in MAT.
  3. MAT will automatically analyze the heap and provide you with:
    • Leak Suspects Report: A high-level analysis of what is likely causing the memory problem.
    • Dominator Tree: Shows which objects are holding the most memory.
    • Histogram: Lists all classes and the number/size of their instances.

Method C: Using VisualVM (Also Recommended)

VisualVM is another excellent tool that comes bundled with the JDK.

  1. Start VisualVM:
    # Ensure you have JDK installed, then run:
    jvisualvm
  2. In the VisualVM UI, go to File -> Load... and select your core dump file.
  3. VisualVM will load the dump and provide analysis similar to MAT, with a focus on profiling and thread states.

Scenario 3: Java Heap Dump (A Specific Type of "Dump")

Often, when people say "dump java", they mean a heap dump, which is a snapshot of the Java object heap. This is different from a native core dump but is crucial for diagnosing OutOfMemoryError and memory leaks.

How to Generate a Heap Dump

Method 1: jmap (JDK Tool)

# Syntax: jmap -dump:format=b,file=<filename.hprof> <pid>
jmap -dump:format=b,file=java_heap_12345.hprof 12345
  • -dump:format=b specifies the binary format, which is the most common.
  • file=java_heap_12345.hprof is the output file.

Method 2: jcmd (Modern & Recommended)

jcmd is a powerful tool that can perform many diagnostic operations on a running JVM without needing to know the internal flags.

# Syntax: jcmd <pid> GC.heap_dump <filename.hprof>
jcmd 12345 GC.heap_dump java_heap_12345.hprof

How to Analyze a Heap Dump

The analysis process is identical to analyzing a core dump for memory issues. Use Eclipse MAT or VisualVM as described in Scenario 2. They are perfectly suited for .hprof files.


Scenario 4: Java Thread Dump (Another "Dump")

A thread dump shows the state of all threads in the JVM. It's the go-to tool for diagnosing performance issues, deadlocks, and high CPU usage.

How to Generate a Thread Dump

Method 1: jstack (JDK Tool)

# Syntax: jstack <pid>
jstack 12345

This prints the thread dump directly to the console. To save it to a file:

jstack 12345 > thread_dump_12345.log

Method 2: jcmd (Modern & Recommended)

# Syntax: jcmd <pid> Thread.print
jcmd 12345 Thread.print

This will print the thread dump to the JVM's stdout/stderr.

Method 3: kill -3 (or kill -SIGQUIT)

Sending a QUIT signal to the JVM process is a classic way to get a thread dump.

# Syntax: kill -3 <pid>
kill -3 12345

The output will appear in the same place as the JVM's standard output (e.g., the console or a log file configured with -Xloggc: or -XX:LogFile).

Quick Reference Table

Goal Command Best For
Create Core Dump gcore <pid> <file.dump> Saving memory state of a live or crashed process.
Create Heap Dump jcmd <pid> GC.heap_dump <file.hprof> Analyzing OutOfMemoryError and memory leaks.
Create Thread Dump jcmd <pid> Thread.print Finding deadlocks, CPU issues, and thread contention.
Analyze Dump Eclipse MAT or VisualVM The best tools for post-mortem analysis of heap/core dumps.
分享:
扫描分享到社交APP
上一篇
下一篇