In this post we will discuss how to improve the performance of Java programs that utilize ArrayLists by setting the list’s initial capacity.

 

ArrayLists and lists in general are great data structures for storing data. They are convenient for when we don’t know how large our data set. However, this comes with a performance penalty.

 

Take for example the situation where we would like to store incoming data into a list. Depending on the list implementation, every once in a while, new data space has to be allocated in order to store more data. This allocation process can be quite expensive.

 

In the following test, we compared 10 million sequential write and random read operations for a list with and a list without an initial capacity set.

The following results were obtained:

 

 

 

Setting the initial capacity of the array list yielded 10 – 12% increase in sequential write operations. This is because Java did not have to put much effort allocating memory on the fly to accommodate for the new incoming data. Random access read operation performance was not affected.

 

Please also note that setting the initial capacity of the list does not mean that the size of the list is bound to that capacity figure. Your list can still grow beyond that if it needs to.

 

So if system memory is not an issue, why not try to set a larger initial capacity for your Java lists? Or if you know the exact size of your data, then maybe an array would be the best option. 10-12% performance improvement in production environment sounds like a good deal 😉