即使坏人试图抓住它,如何在任何OutOfMemoryException上退出JVM [英] How do I make the JVM exit on ANY OutOfMemoryException even when bad people try to catch it
问题描述
OOME是一类错误,通常你不应该从中恢复。但是如果它被隐藏在一个线程中,或者有人捕获它,那么应用程序可能会进入一个它不会退出的状态,但是没有用处。有关如何防止这种情况的任何建议,即使面对使用可能愚蠢地尝试捕获Throwable或Error / OOME的库? (即你无法直接修改源代码)
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)
推荐答案
解决方案:
-XX:OnOutOfMemoryError="<cmd args>; <cmd args>"
定义:首次抛出OutOfMemoryError时运行用户定义的命令。 (在1.4.2更新12,6中引入)
Definition: Run user-defined commands when an OutOfMemoryError is first thrown. (Introduced in 1.4.2 update 12, 6)
参见 http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
杀死正在运行的进程的示例:
An example that kills the running process:
-XX:OnOutOfMemoryError="kill -9 %p"
这篇关于即使坏人试图抓住它,如何在任何OutOfMemoryException上退出JVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!