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

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

    Details

    • Subcomponent:
    • Resolved In Build:
      b109

      Backports

        Description

        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.

          Attachments

            Issue Links

              Activity

                People

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

                  Dates

                  • Created:
                    Updated:
                    Resolved: