Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I restart JVM on OutOfMemoryError _after_ making a heap dump?

Tags:

java

jvm

openjdk

I know about the -XX:+HeapDumpOnOutOfMemoryError JVM parameter. I also know about -XX:OnOutOfMemoryError="cmd args;cmd args" and that kill -3 <JVM_PID> will request a heap dump.

Question: How can I make sure that I, on OutOfMemoryError, first make a full heap dump and then force a restart (or kill) after the dump is done? Is my best bet -XX:OnOutOfMemoryError="kill -3 %p;sleep <time-it-takes-to-dump>;kill -9 %p"?

like image 795
Ztyx Avatar asked May 29 '15 12:05

Ztyx


People also ask

Can the JVM recover from an OutOfMemoryError without a restart?

No you don't have to restart. But it is probably wise to, especially if you don't have a good / automated way of checking that the service is running correctly. The JVM will recover just fine.

How do I get rid of heap dump on memory error?

In order to capture a heap dump automatically, we need to add the HeapDumpOnOutOfMemoryError command-line option that generates a heap dump when a java. lang. OutOfMemoryError is thrown.


2 Answers

java -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError="kill -9 %p" TestApp

JVM will dump heap first, and then execute OnOutOfMemoryError commands (proof).

like image 122
apangin Avatar answered Sep 28 '22 05:09

apangin


If you just want to shutdown you can use one of the following parameters:

  • -XX:+ExitOnOutOfMemoryError
  • -XX:+CrashOnOutOfMemoryError

The VM arguments were added in Java version 8u92, see the release notes.

ExitOnOutOfMemoryError
When you enable this option, the JVM exits on the first occurrence of an out-of-memory error. It can be used if you prefer restarting an instance of the JVM rather than handling out of memory errors.

CrashOnOutOfMemoryError
If this option is enabled, when an out-of-memory error occurs, the JVM crashes and produces text and binary crash files.

Enhancement Request: JDK-8138745 (parameter naming is wrong though JDK-8154713, ExitOnOutOfMemoryError instead of ExitOnOutOfMemory)

like image 42
flavio.donze Avatar answered Sep 28 '22 05:09

flavio.donze