假设有一个大型 Java 应用程序,如果发生 OutOfMemory
错误,我想优雅地关闭它。现在我看到以下问题:
- 一些组件捕获并“吞噬”所有
Throwables
,包括OutOfMemory
- 如果一个线程没有捕捉到
OutOfMemory
它就会退出,我们不知道OutOfMemory
发生了
我能否以某种方式注册一个“JVM 级”处理程序来捕获可能发生在 JVM 中的任何 OutOfMemory
?
最佳答案
Some components catch and "swallow" all Throwables including OutOfMemory
他们不应该那样做,我会解决他们。
If a thread does not catch OutOfMemory it exits and we do not know that OutOfMemory occurred
我也会解决这个问题。
您可以注册一个 UncaughtExceptionHandler。
唯一的选择可能是在关闭程序的 OOME 上触发脚本。这可以从命令行完成。
关于java - 如何在 JVM 级别捕获 OutOfMemory 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11948088/