A heap dump is a snapshot of all objects in a process's heap memory at a given moment, including their sizes and references. A thread dump captures the state and stack trace of every thread in the process. Heap dumps help find memory leaks and excessive memory use, thread dumps help diagnose hangs, deadlocks and thread pool exhaustion.
What are heap dumps and thread dumps?
Metrics tell you that a process uses too much memory or stopped responding. A heap dump or a thread dump tells you why. Both are snapshots of a running process, taken at one moment, and both are classic tools from the JVM world that also exist for .NET, Node.js and other runtimes.
- A heap dump contains every object on the heap: its class, its size and the references to other objects. It answers questions such as "which objects fill the memory, and who keeps them alive?"
- A thread dump contains every thread with its name, state and current stack trace, plus the locks it holds or waits for. It answers "what is the process doing right now, and why is nothing moving?"
Heap dumps: analyzing memory
A heap dump is the standard way to investigate a memory leak. Analysis tools compute the retained size of each object, meaning the memory that would be freed if that object were collected, and the dominator tree, which shows which objects keep large parts of the heap alive. Typical findings are ever-growing caches without eviction, static collections, listener registrations that are never removed or large result sets loaded into memory at once.
Common ways to create a heap dump:
# Java: on demand
jcmd <pid> GC.heap_dump /tmp/app.hprof
jmap -dump:live,format=b,file=/tmp/app.hprof <pid>
# Java: automatically on OutOfMemoryError
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dumps
# .NET
dotnet-dump collect -p <pid>
dotnet-gcdump collect -p <pid>
In Node.js, v8.writeHeapSnapshot() or the flag --heapsnapshot-signal write a heap snapshot that Chrome DevTools can open. For Java, Eclipse Memory Analyzer (MAT) and VisualVM are widely used analysis tools.
Thread dumps: analyzing hangs
A thread dump is cheap to create and the first thing to take when a service is slow or hangs. In Java you get one with jcmd <pid> Thread.print, jstack <pid> or by sending kill -3, which prints the dump to stdout. Each thread appears with its state:
| State | Meaning |
|---|---|
RUNNABLE | Executing or ready to run, including waiting for I/O in native code |
BLOCKED | Waiting to enter a monitor (synchronized) held by another thread |
WAITING / TIMED_WAITING | Waiting for a signal, a lock, a queue or a timeout |
Typical patterns: many request threads BLOCKED on the same lock point to contention; all worker threads waiting in a connection pool point to an exhausted pool; two threads each holding a lock the other wants form a deadlock, which the JVM reports explicitly as "Found one Java-level deadlock". Because one dump is only a moment, take three to five dumps a few seconds apart. Threads that stay in the same frame across all of them are the interesting ones.
Using dumps safely in production
- Pauses: creating a heap dump stops the JVM for the duration of the write, which can take seconds to minutes for large heaps. Remove the instance from the load balancer first if possible.
- Size and disk: a heap dump is roughly as large as the used heap. In containers, write to a volume with enough space, otherwise the dump fails or fills the node. A container killed by an OOM kill gets no chance to write a dump, so watch memory before the limit is reached.
- Sensitive data: heap dumps contain everything in memory, including passwords, tokens and personal data. Treat them like production databases: restricted access, encrypted storage, short retention.
- Know the GC first: high memory is not always a leak. Check garbage collection metrics: if memory returns to a stable baseline after full collections, there is no leak.
Dumps versus continuous profiling
Dumps are point-in-time and manual. Continuous profiling samples CPU, allocations and locks permanently with low overhead and shows the result as a flame graph over time. That makes it easier to see which code allocates more memory after a release or where threads wait for locks, often without taking a dump at all. Dumps remain the tool of choice for detailed object graphs and hard hangs.
How Obseria helps
Obseria shows heap usage, GC activity and thread counts from OpenTelemetry runtime metrics per instance, and continuous profiling adds allocation and lock profiles in production. Rising memory, long GC pauses or blocked threads are visible early, so you can take a dump at the right moment and on the right instance.