Ztyx Ztyx - 15 days ago 8
Java Question

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

I know about the

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"
?

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

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