1

I get an OutOfMemoryError from java.lang.StringBuilder.append even when I set the memory size to 32 GB and reading only a 500 MB file into the builder.

java.lang.OutOfMemoryError: null
    at java.base/java.lang.AbstractStringBuilder.hugeCapacity(AbstractStringBuilder.java:214) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.newCapacity(AbstractStringBuilder.java:206) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:173) ~[na:na]
    at java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:538) ~[na:na]
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:174) ~[na:na]
    at com.github.loa.indexer.service.index.base64.Base64Encoder.encode(Base64Encoder.java:31) ~[classes/:na]
5
  • 3
    It would be great do add code snippet to get more context as to what is happening. Commented May 30, 2019 at 19:02
  • My use case is quite complex. Trying to stream a file into byte arrays in 3072-byte long chunks, then encode the chunks with java.util.Base64, append the result of the encoding to a StringBuilder. Sadly the StringBuilder dies quickly. I need to get the whole base64 string into the memory to send it to Elasticsearch (streaming is not an option). The code is available here: github.com/bottomless-archive-project/library-of-alexandria/… Commented May 30, 2019 at 19:16
  • What version of Java are you running? Commented May 30, 2019 at 19:38
  • @Andreas Java 11. I think it specific to Java 9+ but not sure. Commented May 30, 2019 at 20:07
  • Kinda sloppy that they throw an OOM. Now I need to catch it somewhere in my code and I know, catching OOMs are quite bad. Commented May 30, 2019 at 20:09

1 Answer 1

5

This happens because the byte array that the StringBuilder want to allocate would require a larger array size than allowed by the JVM (more than 2147483647 aka Integer.MAX_VALUE elements).

Sign up to request clarification or add additional context in comments.

1 Comment

Said another way, it's a limit of Java that strings cannot exceed 2 GB in size, which generally means that it cannot exceed 1 billion characters (latin1-only strings can be 2 billion characters in later versions of Java).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.