Video in progress

Strong Junior ⚙️ JVM and memory

What does turning on -XX:+HeapDumpOnOutOfMemoryError in a prod service give you, and what should you watch for?

The state of the heap at the exact moment of the OOM is written to a file, and then you can see precisely what occupied the memory. What to watch for: disk space for the dump, the file path surviving the container's death, and the sensitive data inside.

📖 Answer

An OOM is a hard error to reproduce. It often shows up at night, under a certain load. Without a dump, you are only guessing.

java -XX:+HeapDumpOnOutOfMemoryError \
     -XX:HeapDumpPath=/dumps \
     -XX:+ExitOnOutOfMemoryError \
     -jar app.jar

What to look at:

  • disk: the dump is as large as the heap. A 4 GB heap needs that much free space, otherwise the dump is written incompletely;
  • container: /dumps has to be attached to a volume. When the pod restarts, a file inside the container is lost;
  • time: the service doesn't respond until the dump is written, so account for that in the liveness timeout so the health check doesn't kill it;
  • confidentiality: the dump contains passwords, tokens and customer data. Keep it in a safe place and delete it when you are done.

-XX:+ExitOnOutOfMemoryError is a useful addition: after an OOM the JVM doesn't stay half-alive; it exits right away and the orchestrator restarts it.

Set these flags in advance: setting them after an OOM is too late.