How do I make the JVM exit on ANY OutOfMemoryException even when bad people try to catch it(即使坏人试图抓住它,我如何在任何 OutOfMemoryException 上使 JVM 退出)
问题描述
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)
推荐答案
用户 @dennie 发表了应该真的评论成为自己的答案.较新的 JVM 功能使这变得容易,特别是
User @dennie posted a comment which should really be its own answer. Newer JVM features make this easy, specifically
-XX:+ExitOnOutOfMemoryError
在 OOME 上退出,或崩溃:
to exit on OOME, or to crash:
-XX:+CrashOnOutOfMemoryError
自 Java 8u92 https://www.oracle.com/java/technologies/javase/8u92-relnotes.html
Since Java 8u92 https://www.oracle.com/java/technologies/javase/8u92-relnotes.html
这篇关于即使坏人试图抓住它,我如何在任何 OutOfMemoryException 上使 JVM 退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:即使坏人试图抓住它,我如何在任何 OutOfMemoryException 上使 JVM 退出
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01