← Recipes

Crash and OOM Forensics

Capture useful artifacts at the moment a process runs out of Java heap or the VM fails.

Flags

-XX:+HeapDumpOnOutOfMemoryError-XX:HeapDumpPath-XX:ErrorFile

-XX:+HeapDumpOnOutOfMemoryError writes an HPROF heap dump when Java heap is exhausted. -XX:ErrorFile controls where the VM writes fatal error logs such as hs_err_pid files.

Flag details

-XX:+HeapDumpOnOutOfMemoryError
Writes a heap dump when the JVM throws an OutOfMemoryError due to Java heap exhaustion. The dump captures object graphs for offline leak analysis.
-XX:HeapDumpPath=/var/log/myapp/dumps
Chooses the output directory or file for heap dumps. The JVM process must have write permission and enough disk space.
-XX:ErrorFile=/var/log/myapp/hs_err_pid%p.log
Chooses where fatal JVM error logs are written. %p expands to the process ID, which prevents multiple crashes from overwriting the same file.

When to use it

Use this in production services, batch jobs, and staging systems where a failure should leave enough evidence for later analysis with tools such as Eclipse MAT, JDK Mission Control, or VisualVM.

Make sure the dump path has enough disk space. A heap dump can be close to the live heap size.

Java version support

-XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath, and -XX:ErrorFile are longstanding HotSpot diagnostic options and apply across the cataloged Java versions.

Related JEPs

Examples

java -XX:+HeapDumpOnOutOfMemoryError \
  -XX:HeapDumpPath=/var/log/myapp/dumps \
  -XX:ErrorFile=/var/log/myapp/hs_err_pid%p.log \
  -jar app.jar

Use absolute paths in production so dumps land on a volume with known retention and capacity.

Verify

Confirm write permissions and retention policy for dump directories before relying on the flags during an incident.