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.

          Issue Links

            Activity

            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/123593aacb48
            User: igerasim
            Date: 2016-03-02 11:11:24 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/123593aacb48 User: igerasim Date: 2016-03-02 11:11:24 +0000
            Hide
            coffeys Sean Coffey added a comment -
            Example stack trace :

            Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
                    at java.util.Arrays.copyOf(Arrays.java:3332)
                    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137)
                    at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121)
                    at java.lang.AbstractStringBuilder.ensureCapacity(AbstractStringBuilder.java:111)
                    at java.lang.StringBuilder.ensureCapacity(StringBuilder.java:76)
            Show
            coffeys Sean Coffey added a comment - Example stack trace : Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit         at java.util.Arrays.copyOf(Arrays.java:3332)         at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137)         at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121)         at java.lang.AbstractStringBuilder.ensureCapacity(AbstractStringBuilder.java:111)         at java.lang.StringBuilder.ensureCapacity(StringBuilder.java:76)
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/123593aacb48
            User: lana
            Date: 2016-03-09 18:29:48 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/123593aacb48 User: lana Date: 2016-03-09 18:29:48 +0000

              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: