java -XX:+HeapDumpOnOutOfMemoryError 未在 OOM 中创建 hprof 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1518839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:53:08  来源:igfitidea点击:

-XX:+HeapDumpOnOutOfMemoryError not creating hprof file in OOM

javahprof

提问by Persimmonium

I start my java code (1.6.0_16 in Vista) with the following params (among others) -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs. I run the code and I can see in the logs there are two OOM.

我使用以下参数(以及其他参数)启动我的 Java 代码(Vista 中的 1.6.0_16)-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs。我运行代码,我可以在日志中看到有两个 OOM。

The first one I know cause I can see in the stdout that the hprof file is being created:

我知道的第一个原因是我可以在标准输出中看到正在创建 hprof 文件:

java.lang.OutOfMemoryError: Java heap space
Dumping heap to ../logs\java_pid4604.hprof ...
Heap dump file created [37351818 bytes in 1.635 secs]

And then, towards the end of the code I get another OOM, I capture this, but I don't get a second hprof file created. Anybody knows why is that?? Is it because I have captured the OOM exception?

然后,在代码的末尾,我得到另一个 OOM,我捕获了它,但没有创建第二个 hprof 文件。有人知道这是为什么吗??是不是因为我捕捉到了OOM异常?

采纳答案by Jim Nordlander

Out of memory generates only one dump-file on the first error. If you want to get more you can try jmap or keep jconsole on the jvm (version 6) then you can after everything crashed i.e in the morning create your own dump from jconsole (or your analyser tool of choice).

内存不足在第一个错误时只生成一个转储文件。如果您想获得更多,您可以尝试 jmap 或将 jconsole 保留在 jvm(版本 6)上,然后您可以在一切崩溃后即在早上从 jconsole(或您选择的分析器工具)创建您自己的转储。

More on the dumping subject can be read in Eclipse MemoryAnalyser.

可以在Eclipse MemoryAnalyser 中阅读有关转储主题的更多信息

回答by sfussenegger

I wouldn't try to recover from an OutOfMemoryError as some objects might end up in an undefined state (just thinking about an ArrayList that couldn't allocate its array to store date for instance).

我不会尝试从 OutOfMemoryError 中恢复,因为某些对象可能最终处于未定义状态(例如,仅考虑无法分配其数组来存储日期的 ArrayList)。

Regarding your question, I'd suspect that -XX:+HeapDumpOnOutOfMemoryError is only creating a single dump intentionally to prevent multiple heap dumps: just think about several threads throwing an OOME at the same time, causing a heap dump for each thrown exception.

关于您的问题,我怀疑 -XX:+HeapDumpOnOutOfMemoryError 只是故意创建一个转储以防止多个堆转储:只需考虑多个线程同时抛出 OOME,为每个抛出的异常导致堆转储。

As a summary: don't try to recover from OOME and don't expect the JVM to write more than a single heap dump. However, if you still feel the need to generate a heap dump, you could try to manually handle an OOME exception and call jmap to create a dump or use "-XX:+HeapDumpOnCtrlBreak" (not sure though, how to simulate CtrlBreak programmatically).

总结:不要尝试从 OOME 中恢复,也不要期望 JVM 写入多个堆转储。但是,如果您仍然觉得需要生成堆转储,您可以尝试手动处理 OOME 异常并调用 jmap 来创建转储或使用“-XX:+HeapDumpOnCtrlBreak”(虽然不确定,如何以编程方式模拟 CtrlBreak) .