2

I want to figure out sometihng.

is this:

    List <String> list = new ArrayList<String>();
    list.add("abc");
    list.add(null);

equals to this

    List <String> list = new ArrayList<String>();
    list.add("abc");

in memory usage?

thanks!

7 Answers 7

5

The initial capacity of an ArrayList is ten (references). That is to say, the underlying array will be of size ten, even if you've only got one entry in your collection. Those references will default to null, and consequently setting the second reference to null will neither affect the internal state of the arraylist (in terms of the underlying array) nor its memory consumption.

If you'd added an eleventh item (set to null), the ArrayList would expand its capacity, and consequently you'd consume more memory, but rather because the ArrayList had created extra buckets for your String references.

From the doc linked above:

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.

Sign up to request clarification or add additional context in comments.

Comments

0

In this case yes, as ArrayList allocates an array of 10 positions by default (in openjdk).

If you used LinkedList instead, then the answer would be no.

Comments

0

Maybe, the first list has two elements and the second list has one element so the first list is larger. However most lists default to 10 elements the two lists may be the same size but if the second add required the list to be expanded then it will take more memory.

Comments

0

Well, "it depends".

With the code you have there, running on the Sun JVM, and the way that they've implemented ArrayList, the answer is "yes", they're equal.

This is because by default, an ArrayList starts off with an array of 10 elements. Odds are the most implementations do the same thing.

But if you had an ArrayList with 10 elements, and another with 11, whether the elements are null or not, the 11 element one would consume more RAM because the internal array used to track the elements would have expanded.

Finally, if you were using different List implementation, such as a LinkedList, then the two lists would consume different amounts of memory, as the LinkedList doesn't pre-allocate anything and uses a node wrapper for its elements.

Comments

0

Edit: Changed my mind. As many people are stating, yes they will take up the same amount of memory at first, once you add enough null's to the point where the initial capacity is reached you will increase your memory size.

I originally thought that because you can access the null in the first list that it would lead to more memory, however the null values are there in both cases, and the ability to access them does not affect memory allocation.

Comments

0

In this specific case, using an ArrayList with two elements they do.

The initial capacity for an ArrayList is 10 elements, so as long as you add 10 or fewer elements the size of the ArrayList itself is the same (size in memory, not size()). When you add the 11th element the the ArrayList will have to grow the internal storage and thus take more memory.

2 Comments

Size changes, capacity stays same.
By size I meant the size of the ArrayList in memory, clarified the answer to avoid confusion.
-2

No both are not Equals.

list [abc,null]

Another list stored

list [abc]

so both are not equal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.