在线程的 run() 中捕获 OutofMemoryError 是否明智?

发布于 2024-12-10 17:00:32 字数 75 浏览 0 评论 0原文

我怀疑在我的应用程序中内存不足错误导致 aa run() 退出,但是因为没有日志,所以错误不可见。

这种情况我该怎么办?

I suspect in my application that an outofmemoryerror is causing a a run() to exit, however because there were no logs, the error wasn't visible.

What should i do in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌面超妹 2024-12-17 17:00:32

您应该使用以下标志运行 JVM:-XX:+HeapDumpOnOutOfMemoryError 以查看发生 OOM 时 JVM 内部发生的情况。这将编写一个 HPROF 文件,您可以使用探查器对其进行分析,Eclipse MAT 是一个很好的选择。使用 -XX:HeapDumpPath=/tmp 配置写入 HPROF 的路径。

You should run your JVM with this flag: -XX:+HeapDumpOnOutOfMemoryError to see whats happening inside the JVM while the OOM happens. This will write a HPROF file, which you can analyze with a profiler, Eclipse MAT is a good one for this. Use -XX:HeapDumpPath=/tmp to configure the path where to write the HPROF to.

林空鹿饮溪 2024-12-17 17:00:32

我不了解其他人,但从不(恕我直言)catch抛出任何扩展错误。 javadoc 中的以下声明指出:

方法不需要在其 throws 子句中声明任何
执行过程中可能抛出的 Error 子类
方法但未被捕获,因为这些错误是异常情况
应该永远不会发生

Error 会与 System.err 一起打印,如果您想避免 OutofMemoryException,请增加堆空间。

I don't know about others, but never (IMHO) catch or throws anything that extends Error. The followig statement from the javadoc states:

A method is not required to declare in its throws clause any
subclasses of Error that might be thrown during the execution of the
method but not caught, since these errors are abnormal conditions that
should never occur.

An Error is printed with System.err and if you want to avoid OutofMemoryException, increase your heapsace instead.

夜空下最亮的亮点 2024-12-17 17:00:32

我不明白为什么你不能这样做,因为父线程和子线程都在同一个 JVM 中生成,并且它们共享相同的堆内存。尽管如此处所述,很少有捕获 OOM 错误的要求

I do not see a reason as why you can not do that as parent and child threads all are spawned in the same JVM and they share the same heap memory. There are rare requirements to catch OOM errors though as explained here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文