我们必须处理一个执行以下操作的类库
catch(Throwable e) {log.error("some message", e)}
但忽略该问题。除了如 https://stackoverflow.com/a/3878199/2954288 中所述运行外部命令之外,JVM中是否有一些内部全局状态可以查询以查看自启动以来是否发生了OutOfMemoryError?
我的问题不重复:Is it a bad practice to catch Throwable? 。我不是问是否或
我们不应该catch(Throwable)
。我想问是否存在某种处理方法。
最佳答案
Is there some internal global state in the JVM that can be queried to see if an OutOfMemoryError has happened since the startup
是的,有一个变量out_of_memory_reported
。它是内部,不应该从外部读取。尽管您可以使用 gdb
来完成此操作,例如:
$ gdb -p PID
(gdb) p 'report_java_out_of_memory(char const*)::out_of_memory_reported'
$1 = 0
如果您想要一种可靠的方法来拦截 Java 应用程序中的所有 OutOfMemoryErrors
,无论它们是否被捕获,您都可以使用 JVMTI Exception Callback 。
示例可参见 here .
关于java - 记录OutOfMemoryError的JVM中的全局状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39139116/