silver
silver copied to clipboard
Out of memory errors give a bad error message in Java 11+
It appears that when an OOM error is encountered, attempting to print the (huge) stack trace when we are already out of memory causes something else to fail prematurely, and we die with an unhelpful error message
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class java.lang.invoke.StringConcatFactory
at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unsafe.java:1042)
at java.base/java.lang.invoke.DirectMethodHandle.shouldBeInitialized(DirectMethodHandle.java:347)
at java.base/java.lang.invoke.DirectMethodHandle.preparedLambdaForm(DirectMethodHandle.java:193)
at java.base/java.lang.invoke.DirectMethodHandle.preparedLambdaForm(DirectMethodHandle.java:210)
at java.base/java.lang.invoke.DirectMethodHandle.make(DirectMethodHandle.java:105)
at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodCommon(MethodHandles.java:2328)
at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodNoSecurityManager(MethodHandles.java:2283)
at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodForConstant(MethodHandles.java:2523)
at java.base/java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:2469)
at java.base/java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:592)
at common.Util.printStackCauses(Util.java:159)
at artifact.Main.main(Main.java:18)
I'm not really sure about the details of what changed or how to fix this. Perhaps we can somehow truncate the stack trace following an out of memory/stack overflow error?
Well, the terrible solution would be to allocate a MB or ten of "useless objects" at the top of main, and free+GC when catching OOMs to give a bit more breathing room...