The trimToSize()
method is an important feature in Java’s ArrayList
class that helps optimize memory usage by reducing the internal capacity of the list to match exactly the number of elements it contains. This is especially useful when you have finished adding elements to the list and want to minimize the memory footprint.
What Is the trimToSize() Method in ArrayList?
- The trimToSize() method trims the capacity of the
ArrayList
instance to be equal to the current size of the list. - It frees any unused allocated space beyond what is necessary to store the elements.
- This method does not affect the number of elements in the list — only the internal storage capacity.
Syntax
public void trimToSize()
Parameters
- The method does not take any parameters.
Return Value
void
: The method does not return any value.
Exceptions
- The method does not throw any exceptions in normal use.
How Does trimToSize() Work Internally?
- Internally,
ArrayList
uses an array to store its elements. - When more elements are added than its current capacity, this backing array is expanded (usually by a factor of 1.5).
trimToSize()
resizes this array down to exactly the current list size, removing any unused space.- This helps reduce memory consumption, especially if the list will no longer grow.
Examples of the trimToSize() Method
1. Trim Capacity of an ArrayList After Adding Elements
import java.util.ArrayList; public class Main { public static void main(String[] args) { // Initial capacity is 50 ArrayList<String> cars = new ArrayList<>(50); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); System.out.println("Before trim: Size = " + cars.size()); cars.trimToSize(); // Trims the capacity to current size (4) System.out.println("After trim: Size = " + cars.size()); System.out.println(cars); } }
Output:
Before trim: Size = 4
After trim: Size = 4
[Volvo, BMW, Ford, Mazda]
Note: The size()
remains 4 before and after trimming; trimming affects the internal capacity, not the visible size.
2. Memory Optimization with trimToSize() method
import java.util.ArrayList; public class MemoryExample { public static void main(String[] args) { // Large initial capacity ArrayList<Integer> numbers = new ArrayList<>(100); numbers.add(10); numbers.add(20); numbers.add(30); // Trim excess capacity to minimize memory usage numbers.trimToSize(); System.out.println("Size of list: " + numbers.size()); // Output: 3 // Capacity has been reduced internally } }
Important Notes
- The default initial capacity of
ArrayList
is 10, but can be set via the constructor. - As elements are added beyond capacity, internal resizing happens automatically.
- You call
trimToSize()
after bulk additions when you know you won’t add more elements to free unused space. trimToSize()
does not affect your ability to add new elements later, but new additions might trigger another resize.- This method is useful for memory-sensitive applications where minimizing heap usage matters.
Summary
Aspect | Details |
---|---|
Method Purpose | Trim internal capacity to current list size |
Syntax | arrayList.trimToSize() |
Return Value | None (void ) |
Parameters | None |
Effect on Size | None; only capacity reduces |
Typical Use Cases | Memory optimization after bulk insertions |
Exceptions | None |
Typical Use Cases
- Reducing memory footprint after loading or modifying data.
- Optimizing applications with many dynamically sized
ArrayLists
. - Performance tuning in large-scale Java applications.