Why does using different ArrayList constructors cause a different growth rate of the internal array?
You get precisely what you asked for, respective what has been specified, even in older versions, where the implementation was different:
ArrayList()
Constructs an empty list with an initial capacity of ten.
ArrayList(int)
Constructs an empty list with the specified initial capacity.
So, constructing the ArrayList
with the default constructor will give you an ArrayList
with an initial capacity of ten, so as long as the list size is ten or smaller, no resize operation will ever be needed.
In contrast, the constructor with the int
argument will precisely use the specified capacity, subject to the growing policy which is specified as
The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
which applies even when you specify an initial capacity of zero.
Java 8 added the optimization that the creation of the ten elements array is postponed until the first element is added. This is specifically addressing the common case that ArrayList
instances (created with the default capacity) stay empty for a long time or even their entire lifetime. Further, when the first actual operation is addAll
, it might skip the first array resize operation. This does not affect lists with an explicit initial capacity, as those are usually chosen carefully.
As stated in this answer:
According to our performance analysis team, approximately 85% of ArrayList instances are created at default size so this optimization will be valid for an overwhelming majority of cases.
The motivation was to optimize precisely these scenarios, not to touch the specified default capacity, which was defined back when ArrayList
was created. (Though JDK 1.4 is the first one specifying it explicitly)
If you use the default constructor, the idea is to try to balance memory usage and reallocation. Hence a small default size (10) is used that should be fine for most applications.
If you use the constructor with an explicit size, it is assumed that you know what you're doing. If you initialize it with 0 you are essentially saying: I am pretty sure this will either stay empty or not grow beyond very few elements.
Now if you look at the implementations of ensureCapacityInternal
in openjdk (link), you can see that only the first time you add an item, this difference comes into play:
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
If the default constructor is used, the size grows to DEFAULT_CAPACITY
(10). This is to prevent too many reallocations if multiple elements are added. However if you explicitly created this ArrayList with size 0, it will simply grow to size 1 on the first element you add. This is because you told it that you know what you're doing.
ensureExplicitCapacity
basically just calls grow
(with some range/overflow checks), so let's look at that:
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
As you can see, it doesn't simply grow to a specific size, but it tries to be smart. The bigger the array is, the bigger it will grow even if minCapacity
is just 1 bigger than the current capacity. The reasoning behind that is simple: The probability that a lof of items will be added is higher if the list is already big and vice versa. This is also why you see growth increments by 1 and then by 2 after the 5th element.