JDK-6933217 : Huge arrays handled poorly in core libraries
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 6u18,7
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,linux
  • CPU: generic,x86
  • Submitted: 2010-03-09
  • Updated: 2020-01-22
  • Resolved: 2011-05-17
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
7 b94Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
see discussion on the core-libs-dev mailing list:
  http://mail.openjdk.java.net/pipermail/core-libs-dev/2010-March/003694.html

Excerpt from this discussion that describes the problem:

 "I've noticed bugs in java.util.ArrayList, java.util.Hashtable and
java.io.ByteArrayOutputStream which arise when the capacities of the data
structures reach a particular threshold.  More below.

When the capacity of an ArrayList reaches (2/3)*Integer.MAX_VALUE its size
reaches its capacity and an add or an insert operation is invoked, the
capacity is increased by only one element.  Notice that in the following
excerpt from ArrayList.ensureCapacity the new capacity is set to (3/2) *
oldCapacity + 1 unless this value would not suffice to accommodate the
required capacity in which case it is set to the required capacity.  If the
current capacity is at least (2/3)*Integer.MAX_VALUE, then (oldCapacity *
3)/2 + 1 overflows and resolves to a negative number resulting in the new
capacity being set to the required capacity.  The major consequence of this
is that each subsequent add/insert operation results in a full resize of the
ArrayList causing performance to degrade significantly.

        int newCapacity = (oldCapacity * 3)/2 + 1;
            if (newCapacity < minCapacity)
        newCapacity = minCapacity;

Hashtable breaks entirely when the size of its backing array reaches (1/2) *
Integer.MAX_VALUE and a rehash is necessary as is evident from the following
excerpt from rehash.  Notice that rehash will attempt to create an array of
negative size if the size of the backing array reaches (1/2) *
Integer.MAX_VALUE since oldCapacity * 2 + 1 overflows and resolves to a
negative number.

    int newCapacity = oldCapacity * 2 + 1;
    HashtableEntry newTable[] = new HashtableEntry[newCapacity];

When the capacity of the backing array in a ByteArrayOutputStream reaches
(1/2) * Integer.MAX_VALUE its size reaches its capacity and a write
operation is invoked, the capacity of the backing array is increased only by
the required number of elements.  Notice that in the following excerpt from
ByteArrayOutputStream.write(int) the new backing array capacity is set to 2
* buf.length unless this value would not suffice to accommodate the required
capacity in which case it is set to the required capacity.  If the current
backing array capacity is at least (1/2) * Integer.MAX_VALUE + 1, then
buf.length << 1 overflows and resolves to a negative number resulting in the
new capacity being set to the required capacity.  The major consequence of
this, like with ArrayList, is that each subsequent write operation results
in a full resize of the ByteArrayOutputStream causing performance to degrade
significantly.

    int newcount = count + 1;
    if (newcount > buf.length) {
            buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
    }

It is interesting to note that any statements about the amortized time
complexity of add/insert operations, such as the one in the ArrayList
javadoc, are invalidated by the performance related bugs.  One solution to
the above situations is to set the new capacity of the backing array to
Integer.MAX_VALUE when the initial size calculation results in a negative
number during a resize."

Comments
https://hg.openjdk.java.net/jdk7/jdk7/jdk/rev/ec45423a4700
22-01-2020

EVALUATION Martin has pushed this to tl/jdk http://hg.openjdk.java.net/jdk7/tl/jdk/rev/ec45423a4700
11-05-2010