In C#, List.TrimExcess() asks the list to give back the room it does not use. A list often keeps a bigger hidden array than it needs, so that Add() is fast. When you are sure you will not add many more items, trimming saves memory.
The result is that Capacity drops close to Count. The items, their order, and Count are not touched. It is the opposite of EnsureCapacity(), and it is a good partner for Clear(), which keeps the old capacity.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.TrimExcess(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Call it with no arguments after you have finished adding items.
C#
List<int> numbers = new List<int>(100); // room for 100
numbers.Add(1);
numbers.Add(2);
numbers.TrimExcess(); // Capacity is now close to 2
Call
What it does
numbers.TrimExcess()
Shrinks Capacity toward Count. Items stay the same.
numbers.Clear(); numbers.TrimExcess();
Empty the list and release nearly all of its memory.
numbers.Capacity = numbers.Count
A manual way to trim. It always reallocates, even for a nearly full list.
Trimming copies the items into a smaller array, which costs O(n). The docs say that TrimExcess does nothing when the list is already at more than about 90 percent of its capacity, because copying is not worth it. Because the exact outcome is an internal detail, test with comparisons such as Capacity < 100, not with exact capacity numbers.
Library declarations
C#
public void TrimExcess();
Parameters
Parameter
Description
None
TrimExcess() takes no parameters.
Return Value
Return Value
Description
void
Nothing is returned. Capacity may be smaller afterwards. Count and the items are unchanged.
C# Examples
Example 1 — Trim a Mostly Empty List
Start with room for 100 items, add only three, then trim. Count and the items stay the same.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> list = new List<int>(100);
list.Add(10);
list.Add(20);
list.Add(30);
Console.WriteLine(list.Capacity >= 100);
list.TrimExcess();
Console.WriteLine(list.Capacity < 100);
Console.WriteLine(list.Count);
Console.WriteLine(string.Join(", ", list));
}
}
Output
True
True
3
10, 20, 30
How It Works
Big room, few items. The constructor asks for room for 100 items, but only three are added, so most of the hidden array is empty.
Before trimming. The first line is True, since Capacity is at least 100.
After trimming. Three items are far below 90 percent of 100, so TrimExcess shrinks the array and Capacity < 100 is True. We do not print the exact new capacity.
Items are safe.Count is still 3 and the list still prints 10, 20, 30.
Example 2 — Nothing to Trim
When the list is already full, there is no excess to give back, so the capacity stays the same.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> list = new List<string>(3);
list.Add("a");
list.Add("b");
list.Add("c");
int before = list.Capacity;
list.TrimExcess();
Console.WriteLine(list.Count == before);
Console.WriteLine(list.Capacity == before);
}
}
Output
True
True
How It Works
Exact fit. The list was created with room for 3 and then holds exactly 3 items, so Count equals Capacity.
No excess. There is no unused room to release. TrimExcess has nothing to do and leaves the array alone.
Same capacity. The second comparison is True, because the capacity did not change.
The 90 percent rule. The same happens when a list is close to full. The call is skipped, since a copy would save almost no memory.
Example 3 — Clear, Then Trim
Clear keeps the old room. Add TrimExcess to release it, and note that the list is still usable.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> big = new List<int>();
for (int i = 0; i < 1000; i++) {
big.Add(i);
}
big.Clear();
Console.WriteLine(big.Capacity >= 1000);
big.TrimExcess();
Console.WriteLine(big.Capacity < 1000);
big.Add(7);
Console.WriteLine(big.Count + " " + big[0]);
}
}
Output
True
True
1 7
How It Works
Clear keeps room. After 1000 adds and a Clear, Count is 0 but the hidden array is still big, so Capacity >= 1000 is True.
Trim releases it. With zero items, the list is far below 90 percent of its capacity, so TrimExcess shrinks it and the second line is True.
Still usable.Add(7) works as normal. The list allocates a new small array when it needs to.
Fresh start. The last line shows 1 7: one item, stored at index 0.
Frequently Asked Questions
It reduces the list Capacity so it is close to Count, releasing unused memory. The items and Count do not change.
When the list is already more than about 90 percent full, the call is skipped, because copying to a smaller array would save very little memory.
Clear removes the items but keeps the capacity. TrimExcess gives back unused capacity. Call both when you want an empty list that uses little memory.
Nearly. Setting the Capacity property always reallocates, while TrimExcess skips the work when the list is almost full.
No. It copies the items every time it trims. Use it once, after a big list has finished growing or has been cleared.