Home Java ArrayList Methods trimToSize() Java ArrayList trimToSize() Method 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.
ArrayList<String> list = new ArrayList<>(1000);
list.add("a");
list.add("b");
list.trimToSize(); // capacity now matches size 2 Call What 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.util.ArrayList<E>
public void trimToSize() Parameters Parameter Description None trimToSize() takes no parameters.
Return Value Return Value Description voidNothing; only internal storage may change.
Important
Trimming does not change what the list contains, so you cannot see its effect through the API. Exactly how much memory is freed is an implementation detail; when the list is empty, the JDK can switch to a shared empty array. The list stays fully usable and grows again when you add.
Compare with Similar Methods Capacity and cleanup:
Method How 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 1. Trim a big list 2. After filtering 3. clear vs trim Example 1 — Trim Unused Room After Loading A list reserved for 1000 cities ends up with three; trim it.
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);
}
} size after trim: 3
contents: [Pune, Kochi, Agra] How It Works Over-reserved. The constructor reserved room for 1000 elements, but only three are added.Trim. trimToSize() replaces the 1000-slot array with one that just fits the three cities.Size unchanged. Trimming never removes elements, so the size is still 3.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.
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);
}
} kept: [25, 50, 75, 100] size=4
after trim and add: [25, 50, 75, 100, 999] How It Works Grown to 100. The loop fills the list with 1 to 100, so its internal array grew well beyond 4 slots.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.Release memory. trimToSize() now cuts the array down to 4 slots.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.
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);
}
} after clear: size=0
after trim: size=0 empty=true
reused: [t] How It Works clear() keeps room. After clear() the size is 0, but the internal array keeps its old length, ready to be refilled.Trim an empty list. trimToSize() on an empty list drops that array entirely; the size and isEmpty() are unaffected.Pick one. Use clear() alone when the list will be refilled soon, and add trimToSize() when it will stay small for a long time.Reusable. The trimmed list accepts new elements as normal, so "t" is added without problems.Frequently Asked Questions What does ArrayList trimToSize() do? It shrinks the internal array so that the capacity equals the current size, freeing unused memory.
Does trimToSize() remove elements? No. The size and the elements stay exactly the same.
When should I call trimToSize()? When a list has finished growing, or has shrunk a lot, and will be kept for a long time without many more adds.
Does clear() free the memory of an ArrayList? clear() removes the elements, but the internal array keeps its length. Call trimToSize() afterwards to release it.
Can I still add elements after trimToSize()? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion