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.