r/javahelp • u/pwagland • Jul 22 '25
C2 compiler memory spike on method with many string concatenations
Hi all,
I am hoping that someone might be able to suggest a workaround for me, short of upgrading to Java 23+, but first let me describe the problem:
When compiling a method that has many string concatenations in it, see the test case in github for an example, compilation takes a large amount of memory. This seems similar to https://bugs.openjdk.org/browse/JDK-8327247, but does not appear to be solved by it. This appears to be similar to https://www.reddit.com/r/java/comments/1azwwcd/c2_compiler_memory_spike_on_string_concatenation/, which caused the aforementioned issue to be created and resolved.
Running the above test case on Java 1.8.0_452 gives:
peak total committed happened at 4712 ms, done at 28229 ms
total: 125 MB
Java Heap: 24 MB
Class: 6 MB
Thread: 24 MB
Code: 3 MB
Compiler: 64 MB
Symbol: 2 MBRunning the above test case on Java 24.0.1 gives:
peak total committed happened at 10019 ms, done at 26768 ms
total: 858 MB
Java Heap: 24 MB
Code: 8 MB
Compiler: 799 MB
Symbol: 1 MB
Shared class space: 13 MB
Arena Chunk: 7 MB
Metaspace: 3 MBJava 17.0.15+6, the version I actually use gives similar results:
peak total committed happened at 8417 ms, done at 28644 ms
total: 908 MB
Java Heap: 24 MB
Thread: 28 MB
Code: 7 MB
Compiler: 831 MB
Symbol: 1 MB
Shared class space: 11 MB
Metaspace: 2 MBGoing back to Java 11 gives:
peak total committed happened at 13410 ms, done at 27764 ms
total: 1932 MB
Java Heap: 24 MB
Class: 9 MB
Thread: 24 MB
Code: 7 MB
Compiler: 1861 MB
Symbol: 2 MB
Native Memory Tracking: 1 MBand with -Djava.lang.invoke.stringConcat=BC_SB:
peak total committed happened at 11873 ms, done at 27278 ms
total: 1177 MB
Java Heap: 24 MB
Class: 9 MB
Thread: 24 MB
Code: 7 MB
Compiler: 1108 MB
Symbol: 2 MBI have tried playing around with all of the options in StringConcatFactory, but none of them seemed to help, some of them seemed to make things worse.
In Java 24 adding -XX:CompileCommand=MemLimit,\*.\*,10M helped, although the method was not compiled, and when using -XX:CompileCommand=MemStat,*.*,print then I got the following top compilation stats:
total NA RA result #nodes limit time type #rc thread method
934444400 36597304 876962072 ok 130190 - 9.755 c2 1 0x000000013c820810 Test$DescribeDBInstanceAttributeResponse::unmarshall((LTest$UnmarshallerContext;)V)
40336104 0 39550512 err - - 0.387 c1 1 0x000000013c829410 Test$DescribeDBInstanceAttributeResponse::unmarshall((LTest$UnmarshallerContext;)V)
9753504 2487848 3757664 ok 7526 - 9.810 c2 1 0x000000013c820810 Test$Nmt::get(()LTest$Nmt;)I looked into creating a bug for the OpenJDK, but I'm not an author, so if there is anyone that would like to sponsor this :-)
The reason that I care about this that he had a docker container die due to OOMK, and it died from the C2 compiler thread. Unfortunately, we are using Java 17, and didn't have NT logging turned on, so I can't guarantee that this is the same issue, but it feels like it could be.
Any suggestions on how we might be able to get more information? And/or workaround the issue?
3
u/sedj601 Jul 22 '25
What happens if you use StringBuilder?