When facing elusive OutOfMemoryError
scenarios in production environments, the -XX:+HeapDumpOnOutOfMemoryError
JVM flag presents both opportunities and challenges. The fundamental tradeoff lies between obtaining crucial debugging information and maintaining production stability.
To configure heap dumps properly, consider these implementation patterns:
# Basic configuration
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/path/to/dumps/
-XX:OnOutOfMemoryError="kill -9 %p"
# Advanced example with size limits
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/var/log/java_heapdumps/
-XX:+ExitOnOutOfMemoryError
-XX:HeapDumpMaxFileCount=5
-XX:HeapDumpMaxFileSize=2G
Key factors for production deployment:
- Disk Space Requirements: A full heap dump equals the max heap size (-Xmx)
- Performance Impact: Dumping pauses the JVM (duration proportional to heap size)
- Security Implications: Heap dumps may contain sensitive data
- Container Environments: Requires persistent volume mounts for dump paths
For environments where heap dumps are prohibitive:
# Lightweight monitoring alternative
-XX:+UseGCOverheadLimit
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-XX:+PrintHeapAtGC
-Xloggc:/path/to/gc.log
A phased approach often works best:
- Enable on canary nodes first
- Implement automated cleanup of old dumps
- Set up monitoring for dump file creation
- Consider using
-XX:+HeapDumpBeforeFullGC
for intermittent issues
Last Thursday at 3:47AM, our production monitoring alerted an OOM crash in the payment processing service. Despite extensive load testing, we couldn't reproduce the 2.3GB memory leak that brought down the JVM. This forced us to reconsider our diagnostic approach.
When enabled via -XX:+HeapDumpOnOutOfMemoryError
, the JVM creates a binary heap dump (hprof file) at the moment of OOM. Combine it with these recommended parameters:
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/var/log/java_heapdumps/
-XX:OnOutOfMemoryError="kill -3 %p"
-XX:+CrashOnOutOfMemoryError
Three critical factors when enabling in production:
- Disk Space: A full heap dump equals your -Xmx setting. For 8GB heap, ensure 10GB free space
- Path Permissions: The JVM process must have write access to target directory
- Performance Impact: Dumping pauses the JVM (expect 10-30 sec for multi-GB heaps)
Instead of always-on dumping, consider conditional triggering:
if (System.getProperty("enable.heap.dump") != null) {
ManagementFactory.getMemoryMXBean().setHeapDumpOnOutOfMemoryError(true);
ManagementFactory.getMemoryMXBean().setDumpHeapOnOutOfMemoryErrorPath(
Paths.get("/diagnostics/heap-" + Instant.now().toString() + ".hprof"));
}
When you get the dump:
- Load into Eclipse MAT or VisualVM
- Check "Leak Suspects" report
- Analyze dominator tree
- Compare with previous dumps using Delta mode
For environments where heap dumps aren't practical:
- Flight recorder:
-XX:StartFlightRecording=dumponexit=true
- JMX polling:
MemoryMXBean.getHeapMemoryUsage()
- APM tools like NewRelic or Dynatrace
After several iterations, we settled on:
JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/mnt/diag-store/heapdumps
-XX:+UseGCOverheadLimit
-XX:OnOutOfMemoryError=/scripts/alert_and_restart.sh
-XX:HeapDumpBeforeFullGC=true"