0

When the JVM (OpenJDK 18) throws an OutOfMemoryError, I want to make sure it stops and we take a heap histogram using jmap.

I achieve that with the flags -XX:-ExitOnOutOfMemoryError -XX:OnOutOfMemoryError=/usr/bin/jmap -histo:live $jpid && kill $jpid &2. Assume that $jpid is the Java process's PID.

If I run /usr/bin/jmap -histo:live $jpid && kill $jpid (ie. without & at the end), jmap hangs indefinitely.

I wonder, why does jmap hang when I don't fork it (using &)?


1. I initially used -XX:+ExitOnOutOfMemoryError and -XX:OnOutOfMemoryError=/usr/bin/jmap -histo:live $jpid. That doesn't work, as the former flag will result in the JVM already being stopped when jmap is called.

2. In reality, the jmap + kill call is wrapped in a very simple shell script that logs some additional lines, but I omitted that for simplicity here.

TTT
  • 6,505
  • 10
  • 56
  • 82
  • I don't think it is hanging, it's just not running the process in the background because you removed the instruction to do so by removing the `&`. So it will exit, when the program ends. I believe that sharing more of the script will help here to understand the context. – Andrés Alcarraz Mar 01 '23 at 21:32
  • 2
    `jmap` communicates with the JVM to create the heap dump. So it’s plausible that it will hang if you let the JVM wait for the completion of `jmap`. – Holger Mar 02 '23 at 13:05

0 Answers0