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

后端 未结 3 1970
耶瑟儿~
耶瑟儿~ 2021-02-01 07:06

I know about the -XX:+HeapDumpOnOutOfMemoryError JVM parameter. I also know about -XX:OnOutOfMemoryError=\"cmd args;cmd args\" and that kill -3 will

相关标签:
3条回答
  • 2021-02-01 07:36

    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.

    ExitOnOutOfMemoryError
    When you enable this option, the JVM exits on the first occurrence of an out-of-memory error. It can be used if you prefer restarting an instance of the JVM rather than handling out of memory errors.

    CrashOnOutOfMemoryError
    If this option is enabled, when an out-of-memory error occurs, the JVM crashes and produces text and binary crash files.

    Enhancement Request: JDK-8138745 (parameter naming is wrong though JDK-8154713, ExitOnOutOfMemoryError instead of ExitOnOutOfMemory)

    0 讨论(0)
  • 2021-02-01 07:45
    java -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError="kill -9 %p" TestApp
    

    JVM will dump heap first, and then execute OnOutOfMemoryError commands (proof).

    0 讨论(0)
  • 2021-02-01 08:00

    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.

    0 讨论(0)
提交回复
热议问题