A DESCRIPTION OF THE REQUEST :
This method eases the recalculation of insufficient array size.
Some VMs reserve some header words in an array.
Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size exceeds VM limit
java.lang.System seems to be an appropriate home.
JUSTIFICATION :
Increasing an array is a common task in the JDK.
Exemplary use case: see bug 7153238
---------- BEGIN SOURCE ----------
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
* @param old the current size of the array
* @param size the requested size (must not be bigger than the old size)
*/
public int secureArraySize(int old, int size) {
// overflow-conscious code
assert (size <= old);
if (size - MAX_ARRAY_SIZE <= 0)
return size;
if (++old < 0) // overflow
throw new OutOfMemoryError
("Required array size too large");
return (old > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}
---------- END SOURCE ----------
This method eases the recalculation of insufficient array size.
Some VMs reserve some header words in an array.
Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size exceeds VM limit
java.lang.System seems to be an appropriate home.
JUSTIFICATION :
Increasing an array is a common task in the JDK.
Exemplary use case: see bug 7153238
---------- BEGIN SOURCE ----------
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
* @param old the current size of the array
* @param size the requested size (must not be bigger than the old size)
*/
public int secureArraySize(int old, int size) {
// overflow-conscious code
assert (size <= old);
if (size - MAX_ARRAY_SIZE <= 0)
return size;
if (++old < 0) // overflow
throw new OutOfMemoryError
("Required array size too large");
return (old > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}
---------- END SOURCE ----------
- relates to
-
JDK-7153238 (coll) Use smaller more optimistic array size increase for AbstractCollection.toArray()
- Open
-
JDK-8246725 Provide MAX_ARRAY_SIZE publicly
- Open