ArrayList trimToSize() Method in Java with Example

Last Updated : 30 Jul, 2026

The trimToSize() method of the ArrayList class reduces the internal capacity of the list to match its current number of elements. It helps optimize memory usage by releasing any unused allocated space without affecting the elements stored in the ArrayList. This method is useful when the list size has stabilized and no significant additions are expected.

  • Does not change the number or order of elements.
  • Helps optimize memory usage by releasing unused capacity.
  • Useful after bulk insertions or deletions when the list size becomes stable.
Java
import java.util.ArrayList;

public class GFG{

    public static void main(String[] args)
    {
        ArrayList<String> names
            = new ArrayList<>(10); // initial capacity 10

        names.add("Ram");
        names.add("Shyam");
        names.add("Hari");

        System.out.println("Size before trim: "
                           + names.size());
        System.out.println(
            "Note: internal capacity > size before trim");

        // Trimming the capacity
        names.trimToSize();

        System.out.println("Size after trim: "
                           + names.size());
        System.out.println(
            "Capacity now matches size internally");
    }
}

Output
Size before trim: 3
Note: internal capacity > size before trim
Size after trim: 3
Capacity now matches size internally

Explanation: In this example, the ArrayList is created with an initial capacity of 10 but contains only 3 elements. Calling trimToSize() reduces the internal capacity to match the current size of the list. The elements remain unchanged, and only the unused memory is released.

Syntax:

public void trimToSize()

  • Parameters: None.
  • Returns: This method does not return any value.

Below is the diagram that represents the working of trimToSize() method of ArrayList.

file

Example: Trimming an Integer ArrayList

Java
import java.util.ArrayList;

public class GFG{

    public static void main(String[] args)
    {
        ArrayList<Integer> numbers
            = new ArrayList<>(8); // initial capacity 8

        numbers.add(2);
        numbers.add(4);
        numbers.add(5);
        numbers.add(6);
        numbers.add(11);

        System.out.println("ArrayList elements: "
                           + numbers);

        // trim the capacity to match the number of elements
        numbers.trimToSize();

        System.out.println("Size after trim: "
                           + numbers.size());
        System.out.println(
            "Internal capacity now matches number of elements");
    }
}

Output
ArrayList elements: [2, 4, 5, 6, 11]
Size after trim: 5
Internal capacity now matches number of elements

Explanation: The ArrayList initially has an internal capacity of 8 but stores only 5 elements. After calling trimToSize(), the internal capacity is reduced to match the current number of elements, while the list contents remain unchanged.

Example: Trimming an Empty ArrayList

Java
import java.util.ArrayList;

public class GFG{
    
    public static void main(String[] args){
        
        ArrayList<String> emptyList = new ArrayList<>();

        System.out.println("Size before trim: " + emptyList.size());

        emptyList.trimToSize(); // trims capacity to 0

        System.out.println("Size after trim: " + emptyList.size());
    }
}

Output
Size before trim: 0
Size after trim: 0

Explanation: Since the ArrayList is empty, calling trimToSize() reduces its internal capacity to match its size (0). The method executes successfully even when the list contains no elements.

Advantages of trimToSize() Method

  • Optimizes memory usage by releasing unused capacity.
  • Does not modify the elements stored in the ArrayList.
  • Helps improve memory efficiency after bulk insertions or deletions.
  • Easy to use and requires no parameters.
  • Useful when the list size is not expected to grow further.
Comment