JDK-8258565 : ArrayList.ensureCapacity specification missing maximum capacity
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 8
  • Priority: P4
  • Status: New
  • Resolution: Unresolved
  • OS: generic
  • CPU: x86_64
  • Submitted: 2020-12-16
  • Updated: 2020-12-17
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Picked up _JAVA_OPTIONS:   -Dawt.useSystemAAFontSettings=gasp
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

A DESCRIPTION OF THE PROBLEM :
Javadoc of method ArrayList.ensureCapacity does not restrict the capacity of the ArrayList. Sample program:

public class A {
   public static void main(String args[]) {
      ArrayList al = new ArrayList();
      al.ensureCapacity(Integer.MAX_VALUE);
   }
}

Steps to reproduce:

1. Compile above program
2. Run it using Oracle HotSpot java

Current result:

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:265)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
	at java.util.ArrayList.ensureCapacity(ArrayList.java:219)
	at A.main(A.java:6)

Expected result:

The ArrayList capacity increased by Integer.MAX_VALUE.



Comments
The maximum limit is not defined in the javadoc for ArrayList.ensureCapacity(int)
17-12-2020