Reputation: 26271
I often use Lists in my Android applications. Right now I am creating a Twitter page which lists a maximum of 50 "tweets" of a user.
I have a List defined like this:
List<Tweet> tweets = new ArrayList<Tweet>(MAX_TWEETS);
Where Tweet
is a custom object type holding twitter update information(text, date, username, etc) and MAX_TWEETS
is a constant integer value(50).
Questions:
What is the benefit of setting the initial capacity of this List
, if any?
Should I bother setting a capacity when i know my list will be this small? When should i/should i not be setting a capacity?
Upvotes: 5
Views: 605
Reputation: 3071
It will allocate the memory at creation and won't have to copy until you go over that.
But honestly, with only 50 objects, the copying won't take much effort anyway, so I highly doubt you'll see any performance gain at all. But, there's no downside to specifying the size, so you might as well do it.
Upvotes: 2
Reputation: 24732
Default capacity of ArrayList is set to 10 (see jdk 1.6 source). That means array of size 10 will be allocated on creation. If you will be adding element number 11 the capacity will increase to 16. Then increase again once you reach 21.
If you don't expect more than 50 elements the array will resize at most 3 times. Given that small number, it really does not matter much. Set it to 50 if it gives you a piece of mind of saving on array copy.
Actually this is correct formula of size increase:
int newCapacity = (oldCapacity * 3)/2 + 1;
Upvotes: 2
Reputation: 13289
ArrayList, as the name suggests, is implemented as an array (as opposed to a linked list). By specifying the initial size, you can prevent having to grow the array when adding elements. This is an expensive operation, a new array must be created and then the existing elements copied. So, if you know the max values ahead of time, you should never have to do this.
In reality, if the size of your list is 50 and there is only one instance of this array, the array will only be expanded a few times, so in this case it might not matter. Still, your approach is good in case you change the variable later.
Upvotes: 2
Reputation: 4995
By default, in Java 6, the size of a List is 10. That is, the system creates ten memory slots in the underlying Array. If you try adding the 11th element, only the Array copy is created. Providing a size improves performance.
Upvotes: 3
Reputation: 10948
The initial capacity helps if you know that you will need exactly that amount. It will create a container that will be able to reference MAX_TWEETS
items. If you do exceed it, the system will create a new list with twice as many items then copy over the original list (which is common enough in Java applications).
Upvotes: 2
Reputation: 10959
Setting the initial capacity can increase the performance when populating the List, and it can also reduce the memory footprint of the List if you never add more than that number of items to the list.
The memory footprint of a List that has grown, and might have a backing array that is larger than the number of items stored can be reduced by invoking trimToSize()
Upvotes: 3
Reputation: 7070
Setting the capcity of the list will just specify how big it will be. the only up side to doing this will be the fact that when u add items that exceed the default size of a list, it wont have to extend the list.
example: you set the list to 25, and the default is 10? (not 100% sure) if you add 24 elements it wont have to grow the list at all. if you left it as the default it will grow it.
hope this helps
Upvotes: 2
Reputation: 240996
What is the benefit of setting the initial capacity of this List?
It will allocate memory of that size internally and when it grows over that it will reallocate memory and will . if we supply proper initial capacity we can have some cpu cycle saved while re adjusting overflows.
Upvotes: 2