在线程的 run() 中捕获 OutofMemoryError 是否明智?
我怀疑在我的应用程序中内存不足错误导致 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用以下标志运行 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.我不了解其他人,但从不(恕我直言)
catch
或抛出
任何扩展错误
。 javadoc 中的以下声明指出:Error
会与System.err
一起打印,如果您想避免OutofMemoryException
,请增加堆空间。I don't know about others, but never (IMHO)
catch
orthrows
anything that extendsError
. The followig statement from the javadoc states:An
Error
is printed withSystem.err
and if you want to avoidOutofMemoryException
, increase your heapsace instead.我不明白为什么你不能这样做,因为父线程和子线程都在同一个 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