简体   繁体   English

如何在OutOfMemoryError _after_上进行堆转储时重新启动JVM?

[英]How can I restart JVM on OutOfMemoryError _after_ making a heap dump?

I know about the -XX:+HeapDumpOnOutOfMemoryError JVM parameter. 我知道-XX:+HeapDumpOnOutOfMemoryError JVM参数。 I also know about -XX:OnOutOfMemoryError="cmd args;cmd args" and that kill -3 <JVM_PID> will request a heap dump. 我也知道-XX:OnOutOfMemoryError="cmd args;cmd args"并且kill -3 <JVM_PID>将请求堆转储。

Question: How can I make sure that I, on OutOfMemoryError , first make a full heap dump and then force a restart (or kill) after the dump is done? 问题:如何确保在OutOfMemoryError首先进行完全堆转储, 然后在转储完成后强制重启(或终止)? Is my best bet -XX:OnOutOfMemoryError="kill -3 %p;sleep <time-it-takes-to-dump>;kill -9 %p" ? 是我最好的选择-XX:OnOutOfMemoryError="kill -3 %p;sleep <time-it-takes-to-dump>;kill -9 %p"

java -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError="kill -9 %p" TestApp

JVM将首先转储堆,然后执行OnOutOfMemoryError命令(证明)

If you just want to shutdown you can use one of the following parameters: 如果您只想关闭,可以使用以下参数之一:

  • -XX:+ExitOnOutOfMemoryError
  • -XX:+CrashOnOutOfMemoryError

The VM arguments were added in Java version 8u92, see the release notes . VM参数在Java版本8u92中添加,请参阅发行说明

ExitOnOutOfMemoryError ExitOnOutOfMemoryError
When you enable this option, the JVM exits on the first occurrence of an out-of-memory error. 启用此选项后,JVM会在第一次出现内存不足错误时退出。 It can be used if you prefer restarting an instance of the JVM rather than handling out of memory errors. 如果您更喜欢重新启动JVM实例而不是处理内存不足错误,则可以使用它。

CrashOnOutOfMemoryError CrashOnOutOfMemoryError
If this option is enabled, when an out-of-memory error occurs, the JVM crashes and produces text and binary crash files. 如果启用此选项,则在发生内存不足错误时,JVM会崩溃并生成文本和二进制崩溃文件。

Enhancement Request: JDK-8138745 (parameter naming is wrong though JDK-8154713 , ExitOnOutOfMemoryError instead of ExitOnOutOfMemory ) 增强请求: JDK-8138745 (参数命名错误,但JDK-8154713ExitOnOutOfMemoryError而不是ExitOnOutOfMemory

I bet the runtime sets a specific errorlevel on crash. 我敢打赌,运行时会在崩溃时设置一个特定的错误级别。 Check for that return code and rerun the program in that case. 检查该返回码并在该情况下重新运行程序。 You should perhaps put that into a script. 您应该将其放入脚本中。

The sun jre allows you to heap dump on oome, perhaps openjdk does too. sun jre允许你在oome上堆积转储,也许openjdk也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM