Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8149330

Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary

XMLWordPrintable

    • b109

        With the current implementation, the following code can trigger OOM, even though there might still be enough room to grow.

                    StringBuilder sb = new StringBuilder();
                    sb.ensureCapacity(Integer.MAX_VALUE / 2);
                    sb.ensureCapacity(Integer.MAX_VALUE / 2 + 1);

        The exception reports:
        Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit,
         which means we tried to allocate Integer.MAX_VALUE elements array.

              igerasim Ivan Gerasimov
              shadowbug Shadow Bug
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: