A HotSpot JVM in a Kubernetes pod throws OutOfMemoryError and gets restarted, but no heap dump survives. How do you make that evidence outlive the restart?
answer
- where does the dump land by default?
- a volume, not the container layer
- dump first, then exit
- every restart is PID 1
- a kernel kill leaves no dump
basics
~10 sEnable -XX:+HeapDumpOnOutOfMemoryError with -XX:HeapDumpPath pointing at a volume that outlives the container, add -XX:+ExitOnOutOfMemoryError so the JVM exits once the dump is written, and size the volume for a whole heap.
solid answer
~50 sBy default the dump is `java_pid<pid>.hprof` in the working directory, inside the container's writable layer, which goes away with the container. Point `-XX:HeapDumpPath` at a directory on a volume: an `emptyDir` survives container restarts within the pod, a persistent volume survives the pod itself. Add `-XX:+ExitOnOutOfMemoryError`: after the dump the JVM prints `Terminating due to java.lang.OutOfMemoryError: ...` and exits with code 3, instead of running on with threads that died mid-task, and the orchestrator restarts it. Two container traps follow. The JVM is usually PID 1, so every restart wants `java_pid1.hprof`, and the out-of-memory dump never overwrites, so the second one fails with `File exists`; give each start its own directory or move old dumps away. And a long dump can outlast the liveness probe. None of this helps when the kernel OOM-kills the process, which leaves no dump at all.
code
yaml · 15 linescontainers:
- name: app
env:
- name: JAVA_TOOL_OPTIONS
value: >-
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/diagnostics
-XX:+ExitOnOutOfMemoryError
volumeMounts:
- name: diagnostics
mountPath: /diagnostics
volumes:
- name: diagnostics
emptyDir:
sizeLimit: 4Gigo deeper
Know the flags by name: HeapDumpOnOutOfMemoryError writes the dump, HeapDumpPath says where, ExitOnOutOfMemoryError makes the JVM exit after it.
Explain why the default location disappears in a container, which volume type survives what, and the order in which the JVM dumps and then exits.
Anticipate the container traps: PID 1 file-name collisions, dump size against the volume, liveness probes killing a long dump, and kernel kills that bypass all of it.
Make the dump path, volume size, access rules and retention part of the platform default, because heap dumps hold whatever secrets and personal data were in memory.
## What the JVM does on OutOfMemoryError When the JVM itself throws an `OutOfMemoryError` (the flag's own description says "thrown from JVM"), HotSpot runs a fixed sequence, once per process, for the first thread that reports it: 1. If `-XX:+HeapDumpOnOutOfMemoryError` is set, it prints `java.lang.OutOfMemoryError: <message>` and writes a heap dump. 2. If `-XX:OnOutOfMemoryError` holds commands, it runs them. 3. If `-XX:+CrashOnOutOfMemoryError` is set, it aborts with a fatal error log and, where limits allow, a core dump. 4. If `-XX:+ExitOnOutOfMemoryError` is set, it prints `Terminating due to java.lang.OutOfMemoryError: <message>` and exits with status **3**, with no shutdown hooks run. The order matters: the dump is written **before** the exit, so the two flags work together. All four are off or empty by default. ## Where the dump lands, and why it vanishes `-XX:HeapDumpPath` names a file or a directory. Without it the file is `java_pid<pid>.hprof` in the JVM's **working directory**; with an existing directory the JVM appends that default name; `%p` in the path expands to the PID. `-XX:HeapDumpGzipLevel` (0 by default, 1 to 9) compresses it, and the default name gains `.gz`. In a container the working directory sits in the container's **writable layer**. When the kubelet restarts a crashed container it starts a fresh one, so the old layer, and the dump in it, is gone. | Destination | Survives a container restart | Survives pod deletion | Notes | |---|---|---|---| | Working directory | no | no | the default | | `emptyDir` on disk | yes | no | counts as the pod's ephemeral storage | | `emptyDir` in memory | yes | no | counts as memory, against the limit you just exceeded | | Persistent volume | yes | yes | needs cleanup and access rules | ## Container traps - **PID 1 every time.** The JVM is usually PID 1 in its container, so `%p` does not help and every start wants `java_pid1.hprof`. The out-of-memory dump opens its file in exclusive mode, never overwriting, so the second dump in the same directory fails with `Unable to create .../java_pid1.hprof: File exists` and is lost. Give each start its own directory (named after the pod and a timestamp by the entrypoint script), or have something move the old dump away. - **Size.** A heap dump runs to roughly the live heap. A volume smaller than that leaves an incomplete file, and outgrowing an `emptyDir`'s `sizeLimit` gets the pod evicted. - **Probes.** Writing a large dump takes seconds to minutes, and request threads do not answer meanwhile. A liveness probe that fails during the dump can get the container killed before the file is complete, so leave the probe enough slack. - **Permissions.** The dump is created readable and writable by its owner only, so whatever copies it out must run as the JVM's user. - **The wrong death.** If the process exceeds its container memory limit, the kernel kills it. No `OutOfMemoryError` is thrown, and none of these flags fires. ## Why exit at all Without `ExitOnOutOfMemoryError`, the error unwinds whichever threads happened to allocate. The JVM keeps running with some workers dead, some locks perhaps never released and caches half-built, while the pod still looks alive. Exiting turns a silent degradation into a restart the platform sees and counts, and because the dump is written first, the evidence is already on the volume. A crash loop also stays visible: the restart count rises, and the dumps on the volume show how often it happened. ## Beyond the heap dump - **A continuous flight recording.** A JFR recording started with `-XX:StartFlightRecording` and writing its output on the same volume keeps the minutes before the failure, which a heap dump alone cannot show: the allocation rate, the GC history, the threads. - **Switching it on later.** `HeapDumpOnOutOfMemoryError` and `HeapDumpPath` are **manageable** flags, so `jcmd <pid> VM.set_flag` can enable them on a running JVM that was started without them. - **On demand.** `jcmd <pid> GC.heap_dump <file>` writes a dump without waiting for a failure. The path is opened by the JVM in its own filesystem, and `-overwrite` is needed to replace an existing file. Reading the dump, and telling an orderly exit from a crash or a kernel kill by the container's exit status, are separate subjects; the job here is that a file exists to read.
- Why does the second heap dump in the same pod fail with File exists?The out-of-memory dump opens its file exclusively and never overwrites. The JVM is PID 1 in every restarted container, so each start computes the same `java_pid1.hprof`. The first dump survives on the `emptyDir`, and it blocks every later one. Use a directory per start, or move the file away when the container starts.
- What can you do for a JVM already running without HeapDumpOnOutOfMemoryError?`HeapDumpOnOutOfMemoryError` and `HeapDumpPath` are manageable flags, so `jcmd <pid> VM.set_flag HeapDumpOnOutOfMemoryError true` and a `HeapDumpPath` on a mounted volume take effect without a restart. `ExitOnOutOfMemoryError` is not manageable, so it waits for the next deploy.
- Why is a memory-backed emptyDir a poor place for heap dumps?Files on a memory-backed `emptyDir` are counted as memory use. Writing a dump of most of the heap there, right after the JVM ran out of memory, can push the pod over its limit and get it killed by the kernel mid-write. A disk-backed `emptyDir` or a persistent volume avoids that.
- ExitOnOutOfMemoryError makes the JVM exit before it can write the heap dump.
- Pointing HeapDumpPath at a directory gives every restart's dump a unique name.
- Kubernetes keeps the old container's files after a restart.
- A kernel OOM kill still triggers HeapDumpOnOutOfMemoryError.
- A memory-backed emptyDir is the fastest, safest place for dumps.