The garbage collector
The managed heap
Every object created with new for a reference type lives on the managed heap, and the garbage collector frees it once nothing can reach it any more. A program does not free memory; it drops the last reference. The first measurement allocates a thousand one-kilobyte arrays and keeps them in a list, so nothing can be collected:
var before = GC.GetTotalMemory(true);
var items = new List<byte[]>();
for (var i = 0; i < 1000; i++)
{
items.Add(new byte[1024]);
}
var after = GC.GetTotalMemory(false);
Console.WriteLine($"before {before}");
Console.WriteLine($"after {after}");
Console.WriteLine($"grew {after - before}");
Console.WriteLine($"items {items.Count}");
dotnet run
before 45912
after 1114968
grew 1069056
items 1000
One thousand arrays of 1024 bytes is 1 024 000 bytes of payload, and the growth is a little above that: every array carries an object header and a length, and the list of a thousand references is there too. GetTotalMemory(true) collects before measuring, so before is the live set with the garbage of the startup removed; false does not collect.
Counting collections
GC.CollectionCount(n) is the number of collections of generation n since the process started. A loop that allocates two hundred thousand short-lived strings makes the young generation fill up and be collected:
Console.WriteLine($"collections before {GC.CollectionCount(0)} {GC.CollectionCount(1)} {GC.CollectionCount(2)}");
var total = 0;
for (var i = 0; i < 200000; i++)
{
var text = new string('x', 100);
total += text.Length;
}
Console.WriteLine($"collections after {GC.CollectionCount(0)} {GC.CollectionCount(1)} {GC.CollectionCount(2)}");
Console.WriteLine($"memory {GC.GetTotalMemory(false)} total {total}");
dotnet run
collections before 0 0 0
collections after 28 0 0
memory 1003096 total 20000000
Twenty-eight gen 0 collections, and none of gen 1 or 2: every string died immediately, so nothing was promoted and the older generations were never touched. The memory stayed at about a megabyte although the loop allocated twenty megabytes in total, which is the collector doing its job: the live set is small, and the rest was reused.
Generations
The heap has three generations. New objects start in generation 0; a collection of generation 0 copies the survivors to generation 1; the next one copies them to generation 2. An object that the program keeps is promoted each time:
var kept = new byte[1000];
Console.WriteLine($"small gen {GC.GetGeneration(kept)}");
GC.Collect();
Console.WriteLine($"after 1 {GC.GetGeneration(kept)}");
GC.Collect();
Console.WriteLine($"after 2 {GC.GetGeneration(kept)}");
Console.WriteLine($"bytes {kept.Length}");
dotnet run
small gen 0
after 1 1
after 2 2
bytes 1000
The two GC.Collect() calls forced a full collection each, and the array survived both because the variable still refers to it, so it was promoted. Generation 0 is collected often and cheaply, generation 2 rarely and expensively; that is the whole design, and it is why a short-lived object costs almost nothing.
The large object heap
An allocation of 85 000 bytes or more goes to the large object heap, which is collected with generation 2 and is not compacted by default. Such an object starts its life as generation 2:
var small = new byte[1000];
var large = new byte[100000];
Console.WriteLine($"small gen {GC.GetGeneration(small)}");
Console.WriteLine($"large gen {GC.GetGeneration(large)}");
Console.WriteLine($"large bytes {large.Length}");
dotnet run
small gen 0
large gen 2
large bytes 100000
The two arrays differ only in size and already live in different generations. A program that allocates and drops a hundred-kilobyte buffer per request pays a generation 2 collection for each burst, which is why a buffer of that size is pooled with ArrayPool<byte>.Shared instead of allocated again. Next lesson forces collections and watches an object disappear.
Exercises
- Clear the list in the first program and call GC.GetTotalMemory(true) again. How much is left?
- Allocate a thousand objects that survive into the second program and see whether generation 1 or 2 collections appear.
- Measure the same loop with new byte[100] instead of a string and compare the collection counts.
- Find the size at which an array stops being generation 0 by trying 80 000 and 90 000 bytes.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What frees a managed object?
Answer
The garbage collector, once nothing refers to it
C# has no delete: the collector frees an object when it is unreachable.
-
What does GC.GetTotalMemory(true) do?
Answer
Collects first, then reports the live set
The argument asks for a collection before the measurement.
-
What does this program print?
var x = new object(); Console.WriteLine(GC.GetGeneration(x));Answer
0
A small new object is allocated in generation 0.
-
What happens to an object that is still referenced after a collection?
Answer
It is promoted to the next generation
Survivors move up a generation, which is what the measured program showed.
-
Which generation is collected most often?
Answer
Generation 0
Young objects die young, so generation 0 is small and cheap to collect.
-
What does this program print?
Console.WriteLine(GC.CollectionCount(0) >= 0);Answer
True
The count is a number and cannot be negative, so the comparison is true.
-
Where does a 100 000-byte array live?
Answer
On the large object heap, as generation 2
Allocations of 85 000 bytes or more go to the LOH, which is collected with generation 2.
-
Why did the string loop stay at about a megabyte of memory?
Answer
The dead strings were collected and their space reused
The live set stayed small, so collection after collection reused the same space.
-
What does GC.CollectionCount(2) report?
Answer
How many full collections have happened
The method counts collections of the given generation since the process started.
-
What is the usual fix for a hot path that allocates large buffers?
Answer
Reuse a buffer from ArrayPool<byte>.Shared
Pooling avoids both the allocation and the generation 2 collection it would cause.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev