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

后端 未结 10 561
醉梦人生
醉梦人生 2020-12-09 16:32

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 g

10条回答
  •  星月不相逢
    2020-12-09 16:53

    Solution:

    On newer JVMs:

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

    On Older:

    -XX:OnOutOfMemoryError="; "
    

    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"
    

提交回复
热议问题