Search code examples
javaout-of-memory

What does "failed reallocation of scalar replaced objects" mean in Java OOM error?


I have seen this exception message from a third party app (logstash, fwiw)

java.lang.OutOfMemoryError: Java heap space: failed reallocation of scalar replaced objects

There doesn't seems to be a lot of information about this specific variation of the OutOfMemoryError and I wondered whether there is anything worth knowing about how to respond to this variation, apart from the obvious "use less heap" / "allocate more heap".

Logstash was handling excess TRACE output at the time (about 10 JSON formatted events per millisecond) and had deliberately constrained heap, which obviously we will consider increasing.

The JDK --version output is:

openjdk 11.0.23 2024-04-16 LTS
OpenJDK Runtime Environment Corretto-11.0.23.9.1 (build 11.0.23+9-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.23.9.1 (build 11.0.23+9-LTS, mixed mode)

But... what does "failed reallocation of scalar replaced objects" even mean? What is going on internally with in a JVM when it throws this specific exception?


Solution

  • With git clone https://github.com/openjdk/jdk.git, searching for the error message and following the use site back a bit, one ends up at "deoptimization".

    See src/hotspot/share/runtime/deoptimization.cpp where it says things like

    The actual reallocation of previously eliminated objects occurs in realloc_objects, ...

    Another hint comes from wiki.openjdk.org, where it says:

    Deoptimization is the process of changing an optimized stack frame to an unoptimized one

    Putting this together, I gather that during optimization, an object may be moved from the heap to the stack or otherwise "eliminated". Consequently, when the "optimized stack frame" is deoptimized, the object needs room on the heap.

    So "use less heap" / "allocate more heap", as the OP suggests, looks like the only way to improve the situation.