StringBuilder OOMs earlier with JRE 11 compared to JRE 8

Aleksey Shipilev shade at redhat.com
Fri Aug 27 13:35:37 UTC 2021


On 8/27/21 3:25 PM, David Holmes wrote:
>> Is this something that should be looked into or it can be safely ignored
>> (from JDK users POV)?

That is the unfortunate but expected trade-off from the Compact Strings work in JDK 9. That reworked 
the String storage to hold byte[] instead of char[]. When String is in non-Latin1 mode, then 1 
character is placed in 2 bytes. But since both arrays can only hold ~2^32 elements, it means that 
the effective size for a String is twice lower than it used to.

$ jdk11u-dev/bin/java TestSB
Error occurred at length=1073741822 [i=1048575, j=1022]
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

If you keep the String* in Latin1, then the limit is the same at it "used to be", because every 
character takes 1 byte:
  - sb.append((char) j);
  + sb.append((char) (j & 0xFF));

$ jdk11u-dev/bin/java TestSB
Error occurred at length=2147483645 [i=2097151, j=1021]
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

-- 
Thanks,
-Aleksey



More information about the core-libs-dev mailing list