.NET garbage collection and generations
Answer: In the common language runtime (CLR), the garbage collector (GC) serves as an automatic memory manager. It manages the allocation and release of memory for an application, so developers working with managed code don't have to write code to perform memory management tasks. (S1)
What it does
- Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. (S2)
- Eventually the garbage collector must perform a collection in order to free some memory. (S2)
- The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. (S2)
- It reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations. (S1)
- Managed objects automatically get clean content to start with, so their constructors don't have to initialize every data field. (S1)
Generations
- The managed heap is divided into three generations, 0, 1, and 2, so it can handle long-lived and short-lived objects separately. (S1)
- Generation 0 is the youngest and contains short-lived objects, such as a temporary variable. (S1)
- Most objects are reclaimed for garbage collection in generation 0 and don't survive to the next generation. (S1)
- Generation 1 contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects. (S1)
- Because objects that survive collections tend to have longer lifetimes, it makes sense to promote them to a higher generation. (S1)
Claims
- id: C1 claim: The GC is the CLR's automatic memory manager. source: S1 evidence: "the garbage collector (GC) serves as an automatic memory manager."
- id: C2 claim: The managed heap has generations 0, 1, and 2. source: S1 evidence: "the managed heap is divided into three generations, 0, 1, and 2"
- id: C3 claim: Most objects are reclaimed in generation 0. source: S1 evidence: "Objects that survive a generation 0 garbage collection are promoted to generation 1."
- id: C4 claim: New objects are allocated from the managed heap. source: S2 evidence: "the common language runtime allocates memory for the object from the managed heap."
Sources
- [S1] Fundamentals of garbage collection - .NET. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
- [S2] .NET garbage collection - .NET. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/ . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (.NET docs), Fundamentals of garbage collection - .NET, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), .NET garbage collection - .NET, CC BY 4.0 (dotnet/docs).
Cite this
iinventedlight. 2026. ".NET garbage collection and generations." iinventedlight Library, iil:note:apn582my, version of 2026-11-04. https://iinventedlight.com/library/notes/dotnet-garbage-collection/. License: CC-BY-4.0.
@misc{iil_note_apn582my,
author = {{iinventedlight}},
title = {{.NET garbage collection and generations}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/dotnet-garbage-collection/},
note = {Permanent ID iil:note:apn582my. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/dotnet-garbage-collection.md.