Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make the JVM exit on ANY OutOfMemoryException even when bad people try to catch it

Tags:

An OOME is of the class of errors which generally you shouldn't recover from. But if it is buried in a thread, or someone catches it, it is possible for an application to get in a state from which it isn't exiting, but isn't useful. Any suggestions in how to prevent this even in the face of using libraries which may foolishly try to catch Throwable or Error/OOME? (ie you don't have direct access to modify the source code)

like image 772
Michael Neale Avatar asked Oct 06 '10 09:10

Michael Neale


1 Answers

Solution:

On newer JVMs:

-XX:+ExitOnOutOfMemoryError to exit on OOME, or to crash:  -XX:+CrashOnOutOfMemoryError 

On Older:

-XX:OnOutOfMemoryError="<cmd args>; <cmd args>" 

Definition: Run user-defined commands when an OutOfMemoryError is first thrown. (Introduced in 1.4.2 update 12, 6)

See http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

An example that kills the running process:

-XX:OnOutOfMemoryError="kill -9 %p" 
like image 159
Michael Neale Avatar answered Sep 23 '22 14:09

Michael Neale