Java ArrayList trimToSize() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, trimToSize() shrinks the internal array of an ArrayList so its capacity matches the number of elements it holds. The elements, their order, and the size all stay the same.

Use it after a list has finished growing (or after you removed many elements) and will be kept around for a long time, to give unused memory back. If the list will grow again soon, trimming just forces it to regrow.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

No arguments; call it on an ArrayList variable.

Java
ArrayList<String> list = new ArrayList<>(1000);
list.add("a");
list.add("b");
list.trimToSize();     // capacity now matches size 2
CallWhat it does
list.trimToSize()Releases unused capacity.
list.ensureCapacity(n)The opposite: reserves room ahead of time.
list.clear()Removes the elements but keeps the capacity.
new ArrayList<>(list)A copy sized to fit the elements, as an alternative.

Like ensureCapacity, trimToSize is declared only in ArrayList, so the variable must have type ArrayList.

Library declarations
Java
// java.util.ArrayList<E>
public void trimToSize()

Parameters

ParameterDescription
NonetrimToSize() takes no parameters.

Return Value

Return ValueDescription
voidNothing; only internal storage may change.

Compare with Similar Methods

Capacity and cleanup:

MethodHow it differs
ensureCapacity(n)Reserves room instead of releasing it.
clear()Removes all elements but keeps the capacity.
removeIf(filter)Often called before trimming, to drop many elements.
size()What the capacity is trimmed down to.

Java Examples

Example 1 — Trim Unused Room After Loading

A list reserved for 1000 cities ends up with three; trim it.

Java
import java.util.ArrayList;

public class ArrayListTrimToSizeDemo {
    public static void main(String[] args) {
        ArrayList<String> cities = new ArrayList<>(1000);
        cities.add("Pune");
        cities.add("Kochi");
        cities.add("Agra");

        cities.trimToSize();
        System.out.println("size after trim: " + cities.size());
        System.out.println("contents: " + cities);
    }
}

How It Works

  1. Over-reserved. The constructor reserved room for 1000 elements, but only three are added.
  2. Trim. trimToSize() replaces the 1000-slot array with one that just fits the three cities.
  3. Size unchanged. Trimming never removes elements, so the size is still 3.
  4. Contents unchanged. Pune, Kochi, and Agra are still there, in the same order.

Example 2 — Trim After Removing Most Elements

Keep only every 25th ID out of 100, trim, then keep using the list.

Java
import java.util.ArrayList;

public class ArrayListTrimAfterRemove {
    public static void main(String[] args) {
        ArrayList<Integer> ids = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            ids.add(i);
        }

        ids.removeIf(id -> id % 25 != 0);
        ids.trimToSize();
        System.out.println("kept: " + ids + " size=" + ids.size());

        ids.add(999);
        System.out.println("after trim and add: " + ids);
    }
}

How It Works

  1. Grown to 100. The loop fills the list with 1 to 100, so its internal array grew well beyond 4 slots.
  2. Filter. removeIf drops every ID that is not a multiple of 25, leaving 25, 50, 75, and 100. Removing elements does not shrink the array.
  3. Release memory. trimToSize() now cuts the array down to 4 slots.
  4. Still growable. Adding 999 makes the list grow again automatically; trimming does not make it fixed-size.

Example 3 — clear() Keeps Capacity; trimToSize() Releases It

Clear a list, trim it, then reuse it.

Java
import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListTrimAfterClear {
    public static void main(String[] args) {
        ArrayList<String> buffer = new ArrayList<>(Arrays.asList("p", "q", "r", "s"));

        buffer.clear();
        System.out.println("after clear: size=" + buffer.size());

        buffer.trimToSize();
        System.out.println("after trim: size=" + buffer.size() + " empty=" + buffer.isEmpty());

        buffer.add("t");
        System.out.println("reused: " + buffer);
    }
}

How It Works

  1. clear() keeps room. After clear() the size is 0, but the internal array keeps its old length, ready to be refilled.
  2. Trim an empty list. trimToSize() on an empty list drops that array entirely; the size and isEmpty() are unaffected.
  3. Pick one. Use clear() alone when the list will be refilled soon, and add trimToSize() when it will stay small for a long time.
  4. Reusable. The trimmed list accepts new elements as normal, so "t" is added without problems.

Frequently Asked Questions

It shrinks the internal array so that the capacity equals the current size, freeing unused memory.
No. The size and the elements stay exactly the same.
When a list has finished growing, or has shrunk a lot, and will be kept for a long time without many more adds.
clear() removes the elements, but the internal array keeps its length. Call trimToSize() afterwards to release it.
Yes. The list grows again automatically when you add.

Back to the method index

See all 31 ArrayList method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful