JDK-8149330 : Capacity of StringBuilder should not get close to Integer.MAX_VALUE unless necessary
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2016-02-08
  • Updated: 2022-08-04
  • Resolved: 2016-03-02
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8 JDK 9
7u111Fixed 8u102Fixed 9 b109Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
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.

Comments
URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/123593aacb48 User: lana Date: 2016-03-09 18:29:48 +0000
09-03-2016

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)
03-03-2016

URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/123593aacb48 User: igerasim Date: 2016-03-02 11:11:24 +0000
02-03-2016