The trimToSize()
method of ArrayList class is used for memory optimization. It trims the capacity of ArrayList
to the current list size. This method is useful when you want to free the unused storage space after you are done adding elements to an ArrayList
, especially if you have allocated a large capacity initially but are using much less.
For example, If an arraylist is having capacity of 15 but there are only 5 elements in it, calling trimToSize()
method on this ArrayList would change the capacity from 15 to 5.
Syntax
public void trimToSize()
Example
Let’s see an example to understand how to use trimToSize()
method.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// Creating an ArrayList with initial capacity of 100
ArrayList<Integer> list = new ArrayList<>(100);
// Adding elements to the list
list.add(11);
list.add(22);
list.add(33);
list.add(44);
list.add(55);
System.out.println("Size of list: " + list.size());
System.out.println("Capacity before trimToSize: " + getCapacity(list));
// Trim the capacity to the current size
list.trimToSize();
System.out.println("Capacity after trimToSize: " + getCapacity(list));
}
// This method prints the current capacity of ArrayList
private static int getCapacity(ArrayList<?> list) {
try {
java.lang.reflect.Field field = ArrayList.class.getDeclaredField("elementData");
field.setAccessible(true);
return ((Object[]) field.get(list)).length;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
Output:
Size of list: 5
Capacity before trimToSize: 100
Capacity after trimToSize: 5
In this example:
- Initially, we have created an ArrayList with the capacity of 100.
- Only 5 elements are added to this ArrayList.
- After calling
trimToSize()
method, the capacity of ArrayList is reduced to 5, which is the current size of list. - This concludes that using this method can free unused storage space and can be helpful in memory optimization.
Lathy says
Hi,
I ran your code. It prints the same result whether trimToSize() method added or removed.
I even checked the size of the arraylist with and without trimToSize() method but in both case it remains same.
Can you please check and update.
Tony says
@Lathy, There is a difference between size and capacity of an ArrayList. Size is the number of data items stored in an ArrayList. Capacity is the length of the Array that is used internally by the ArrayList. For obvious reasons, Capacity is always maintained greater than or equal to the Size. The trimToSize() method is used in situations where you know the ArrayList size is not likely to increase and you do not want to waste more memory space. Look up the implementation of ArrayList for more info.
Sudarshan says
Good Day Guys,
Can we restrict the size of the Arraylist ?
eg : I should add only 10 objects to the arraylist.
Chaitanya Singh says
Hey Sudarshan, You cannot restrict the size of an ArrayList. Why you want to do that? You could have used array instead of ArrayList if you want the size to be limited.
However if you want to achieve this in ArrayList then you have to limit the size by yourself in the code, something like this would work: