JDK-8294052 : OutOfMemoryErrors thrown by JDK classes do not honor JVM flags for handling OutOfMemoryError
  • Type: Bug
  • Component: hotspot
  • Sub-Component: svc
  • Affected Version: 11,17,20
  • Priority: P4
  • Status: New
  • Resolution: Unresolved
  • Submitted: 2022-09-20
  • Updated: 2022-09-20
Description
Context: we are debugging an OutOfMemoryError from filling up NIO direct memory (-XX:MaxDirectMemorySize). We tried to use -XX:+HeapDumpOnOutOfMemoryError to collect heap dumps, and found out the flag does not work for direct memory OutOfMemoryError.

There are a few JVM flags related to handling OutOfMemoryError:
-XX:+HeapDumpOnOutOfMemoryError
-XX:OnOutOfMemoryError
-XX:+CrashOnOutOfMemoryError
-XX:+ExitOnOutOfMemoryError

The work for handling these flags is in `report_java_out_of_memory(const char*)` in debug.cpp. However, `report_java_out_of_memory()` is only called from HotSpot's C++ code, not when a JDK Java class throws OutOfMemoryError directly.

The case relevant to our direct memory investigation is in java/nio/Bits.java line 175 in `reserveMemory()`. Currently there are 24 occurrences of "throw new OutOfMemoryError" in JDK classes. None of them respects the 4 OutOfMemoryError-handling JVM flags.

I propose we add a JNI function that calls `report_java_out_of_memory()`, possibly in jdk/internal/misc/VM.java. Then add a call to this function before the 24 places of throwing OutOfMemoryError.
Comments
Let me know if you agree that this is a good idea. Should I create a CSR for this change?
20-09-2022