JVM.Tuning.What is the difference between a thread dump and a heap dump?

Difference Between a Thread Dump and a Heap Dump

Thread dumps and heap dumps are both essential for diagnosing JVM performance issues, but they serve different purposes and capture different aspects of the application’s state.

FeatureThread DumpHeap Dump
PurposeCaptures the state of all threads running in the JVMCaptures the state of all objects in the JVM heap memory
Used ForAnalyzing thread behavior, deadlocks, high CPU usageDebugging memory leaks, analyzing object allocations, OutOfMemoryErrors
What It Captures– Thread names, states (RUNNABLE, WAITING, BLOCKED)
– Stack traces of each thread
– Synchronization locks and deadlocks
– Objects in memory
– Class metadata
– Object references
– Memory usage details
File FormatPlain text (.txt or .log)Binary file (.hprof)
How to Generate?jstack, jcmd <PID> Thread.print, kill -3 <PID>jmap -dump, jcmd <PID> GC.heap_dump
How to Analyze?Read manually or use tools like VisualVM, IntelliJ ProfilerLoad into VisualVM, Eclipse Memory Analyzer (MAT), YourKit

1. What is a Thread Dump?

A thread dump is a snapshot of all running threads in a JVM at a specific moment in time. It helps diagnose:

  • Deadlocks (threads waiting on each other indefinitely)
  • High CPU usage (which threads are consuming CPU)
  • Thread contention (which threads are blocking others)
  • Hanging application issues (e.g., stuck threads)

How to Generate a Thread Dump

A. Using jstack (Most Common)

jstack <PID> > thread_dump.txt
  • <PID> is the Java process ID.
  • The output shows all threads, their states, and stack traces.

B. Using jcmd

jcmd <PID> Thread.print > thread_dump.txt

C. Using kill -3 (Linux/Unix)

kill -3 <PID>
  • Sends a SIGQUIT signal, printing the thread dump to stderr (viewable in logs).

How to Analyze a Thread Dump

  • Open in VisualVM or IntelliJ Profiler.
  • Look for:
    • BLOCKED or WAITING states (possible deadlocks)
    • High CPU-consuming threads (RUNNABLE with long stack traces)
    • Too many WAITING threads (indicates contention)

2. What is a Heap Dump?

A heap dump is a snapshot of all objects in memory at a specific time. It helps diagnose:

  • Memory leaks (objects that should be garbage collected but aren’t)
  • Excessive memory consumption (which objects are using too much memory)
  • OutOfMemoryErrors (OOM) (what caused memory exhaustion)

How to Generate a Heap Dump

A. Using jmap





jmap -dump:live,format=b,file=heap_dump.hprof <PID>
  • live → Dumps only objects still in use.
  • format=b → Generates binary format (readable by profiling tools).

B. Using jcmd

jcmd <PID> GC.heap_dump heap_dump.hprof
  • More efficient than jmap and preferred for production environments.

C. On OutOfMemoryError (OOM)

You can configure the JVM to automatically generate a heap dump when an OutOfMemoryError occurs:

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/dumps

How to Analyze a Heap Dump

  1. Open in Eclipse Memory Analyzer (MAT), VisualVM, or YourKit Profiler.
  2. Check for:
    • Large objects or collections consuming excessive memory
    • High object retention (objects that should be garbage collected but aren’t)
    • Memory leaks (objects holding unexpected references)

When to Use Each?

ScenarioUse Thread Dump?Use Heap Dump?
Application is hanging
Application has high CPU usage
Application is running out of memory
Diagnosing deadlocks or contention
Debugging memory leaks
Identifying large memory-consuming objects

Summary

  • Thread Dump: Helps debug thread-related issues (deadlocks, high CPU usage, slow responses).
  • Heap Dump: Helps debug memory-related issues (memory leaks, OutOfMemoryErrors).

Would you like help analyzing a specific performance issue in your Java application? 🚀

This entry was posted in Без рубрики. Bookmark the permalink.