Search code examples
javajfr

How to print jfr when OOME occurs and JVM exits?


I am using Java17 in the container environment.
I set -XX:+ExitOnOutOfMemoryError because I want the JVM to exit when an OutOfMemoryError occurs.
And I also set dumponexit=true to output the JFR on JVM exit.

But when the JVM exits with an OutOfMemoryError, JFR does not print.
Is it possible to set it to output?

Below is an example of a boot parameter.
java -XX:+ExitOnOutOfMemoryError -XX:StartFlightRecording: dumponexit=true app.jar


Solution

  • JFR is dumped in a Java shutdown hook, which doesn't get a chance to run if the JVM exits due to -XX:+ExitOnOutOfMemoryError.

    JFR does something called an emergency dump, a best effort to write contents in memory/disk to a file located next to the hs_err_pid file if the JVM crashes (without executing Java code), possibly due to OOM. It may, or may not, succeed.

    If -XX:+ExitOnOutOfMemoryError is specified, that logic is also prevented from running.