This is low-level memory question about how Java performs .add
and .remove
on an ArrayList
or other types of lists. I would think that Java would have to do a reallocation of memory to append/remove items to a list, but it could be doing something I'm not thinking of to avoid this. Does anyone know?

- 715
- 1
- 6
- 19
-
What is a _regular list_? By _Array lists_, do you mean the `ArrayList` type? Have you looked at its implementation or javadoc? What did you find? – Sotirios Delimanolis Jan 05 '16 at 17:03
-
The only piece I've found mentioning reallocation is "An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation", hence the question. – mjswartz Jan 05 '16 at 17:08
2 Answers
If by "regular list" you mean java.util.List
, that is an interface. It does not specify anything about whether or when any memory is allocated in association with adding or removing elements -- those are details of specific implementations.
As for java.util.ArrayList
in particular, its docs say:
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
In other words, Java does not specify the answer to your question.
If I were to speculate based on the available documentation, I would guess that java.util.ArrayList.remove()
never performs any memory allocation or reallocation. It seems to follow from the docs overall that java.util.ArrayList.add()
allocates additional space at least sometimes (in the form of a new, longer internal array). In order to achieve constant amortized cost for element additions, however, I don't see how it could reallocate on every element addition. Almost certainly, it reallocates only when its capacity is insufficient, and then it scales the capacity by a constant factor -- e.g. doubles it.

- 160,171
- 8
- 81
- 157
-
This ambiguity is what I discovered as well, thanks for the speculation piece though. If anyone gets confused, I edited my question from "... and `.remove` on Array lists or just regular lists." to "... an `ArrayList` or other types of lists." – mjswartz Jan 05 '16 at 18:07
All list implementations require storage of some information about the objects in the list and the order of those objects. Larger lists require more such information because there is some information for each object in the list. Thus adding to a list must, on average, result in allocation of more storage for this information.
Adding an element to a list does not copy the object that was added to the list. Indeed, no Java statements cause an additional copy of an object to be visible to your program (you have to explicitly use a copy constructor or a clone method to do that). This is because Java objects are never accessed directly, but are always accessed through a reference. Adding an object to a collection really means adding a new reference to the object to the collection.

- 46,613
- 43
- 151
- 237