How to get a heap dump?
4. JFR for large Heaps 5. Security — mask PII 6. Be careful with -all=false (Full GC!)
Junior Level
Ways to get a Heap Dump:
# 1. Automatically on OOME (mandatory!)
java -XX:+HeapDumpOnOutOfMemoryError -jar app.jar
# 2. Manually via jcmd
jcmd <PID> GC.heap_dump dump.hprof
# 3. Via VisualVM (GUI)
# → Right-click on process → Heap Dump
Where to find PID:
jps # List of Java processes
# or
ps aux | grep java
Middle Level
jcmd (recommended method)
# Modern standard
jcmd <PID> GC.heap_dump /tmp/dump.hprof
# With compression (Java 11+)
jcmd <PID> GC.heap_dump -gz=1 /tmp/dump.hprof.gz
# → 32 GB → 6-10 GB
# Live objects only (Full GC!)
jcmd <PID> GC.heap_dump -all=false /tmp/live.hprof
Kubernetes
# 1. Enter pod
kubectl exec -it <pod> -- bash
# 2. Capture dump
# In containers, Java may be PID 1 (without init process) or have
# separate PID (with tini/dumb-init or Java 11+ container awareness).
# Check via `jps`.
jcmd 1 GC.heap_dump -gz=1 /tmp/dump.hprof.gz
# 3. Download
kubectl cp <pod>:/tmp/dump.hprof.gz ./dump.hprof.gz
# 4. Cleanup
rm /tmp/dump.hprof.gz
Programmatic Method
// ⚠️ com.sun.management.* — internal JDK API (HotSpot-specific).
// Not guaranteed on non-HotSpot JVM (OpenJ9, GraalVM Native Image).
import com.sun.management.HotSpotDiagnosticMXBean;
public void dumpHeap(String path, boolean live) {
HotSpotDiagnosticMXBean bean = ManagementFactory
.newPlatformMXBeanProxy(
ManagementFactory.getPlatformMBeanServer(),
"com.sun.management:type=HotSpotDiagnostic",
HotSpotDiagnosticMXBean.class);
bean.dumpHeap(path, live);
}
Senior Level
Production Dump Dangers
STW pause → Liveness Probe fail → pod killed!
Solution:
1. Increase probe timeouts
2. Use JFR instead of dump
3. Capture dump on replica
PII and Security
Dump contains:
→ Passwords, tokens, sessions
→ Personal data
→ Transmit only over secure channels
→ RedactHprof for string masking
→ Restrict file access
Arthas
# Alternative to jcmd
curl -L https://arthas.aliyun.com/arthas-boot.jar -o arthas-boot.jar
java -jar arthas-boot.jar
[arthas]$ heapdump /tmp/dump.hprof
→ Finds PID automatically
→ Convenient CLI
Best Practices
- jcmd — official method
- -gz=1 (Java 11+) — compression
- Kubernetes → external volume
- JFR for large Heaps
- Security — mask PII
- Be careful with -all=false (Full GC!)
Senior Summary
- jcmd > jmap (modern standard)
- Gzip (Java 11+) = savings
- Kubernetes → external volume
- JFR > Dump for > 64 GB
- PII = dump contains secrets
- Arthas = convenient alternative
Interview Cheat Sheet
Must know:
- jcmd — official method:
jcmd <PID> GC.heap_dump /tmp/dump.hprof; compression:-gz=1(Java 11+) - Automatic:
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/dumps/— mandatory in production - Kubernetes: enter pod →
jcmd 1 GC.heap_dump -gz=1 /tmp/dump.hprof.gz→kubectl cp→ cleanup -all=false(live): live objects only; requires Full GC before dump → additional STW pause- PII and Security: dump contains passwords, tokens, sessions; transmit over secure channels; RedactHprof for masking
- Production dangers: STW → Liveness Probe fail → pod killed; solutions: increase timeout, use JFR, capture on replica
- Programmatic method:
HotSpotDiagnosticMXBean.dumpHeap()— HotSpot-specific, doesn’t work on OpenJ9/GraalVM Native Image
Common follow-up questions:
- Why is
-gz=1important? — On-the-fly compression: 32 GB → 6-10 GB; saves disk space and transfer time - What if Java is PID 1 in container? — In containers, Java may be PID 1 (without init process);
jcmd 1works; check viajps - Why is programmatic method not cross-platform? —
com.sun.management.HotSpotDiagnosticMXBean— internal JDK API; HotSpot only - When is Arthas better than jcmd? — Arthas finds PID automatically, convenient interactive CLI; good when
jcmdis unavailable
Red flags (DO NOT say):
- “jmap is the best method” — jmap is deprecated; use jcmd
- “Live dump is faster and safer” — Live dump requires Full GC → additional STW pause
- “Dump can be safely stored in Kubernetes ephemeral storage” — ephemeral storage fills with dump → pod won’t restart
Related topics:
- [[23. What is a heap dump]]
- [[22. What tools help analyze memory]]
- [[21. What is a memory leak and how to detect it]]
- [[19. What happens on OutOfMemoryError]]
- [[18. What are -Xms and -Xmx parameters]]