wasAsked.

Generations in Garbage Collection

HCA Healthcare · Role not specified · technical

Published

Year not specified · C#

Community answers

Posted by wasAsked editorial collection

The .NET GC groups managed objects by age. New objects start in generation 0. Objects that survive a collection can be promoted to generation 1 and then generation 2. This generational design is efficient because most allocations die young, so Gen 0 can be collected frequently and cheaply while long-lived objects are scanned less often. Large objects normally go to the large object heap and are collected with Gen 2. ```csharp long before = GC.GetAllocatedBytesForCurrentThread(); var values = Enumerable.Range(0, 10_000).ToArray(); long allocated = GC.GetAllocatedBytesForCurrentThread() - before; Console.WriteLine($"Allocated: {allocated:N0} bytes"); ``` Generation is not an ownership mechanism, and calling `GC.Collect()` in normal application code is usually harmful because it disrupts the GC's adaptive decisions. Dispose unmanaged-resource wrappers promptly with `using`; disposal and garbage collection solve different problems. Diagnose with counters, traces, or dumps, then reduce proven high-volume allocations or unintended retention.

Prince Yadav · · 0 likes

Generations are how the .NET garbage collector organises objects by age to make collection efficient. Why generations exist Collecting the whole heap every time would be slow. Observations show that most objects are short-lived (temporary variables, request objects) while a few live for a long time (caches, configuration, singletons). So the GC groups objects by how many collections they have survived and collects the young groups much more often. The three generations - Gen 0 — newly created objects. Collections happen frequently and are very fast because the area is small and most objects in it are already dead. - Gen 1 — objects that survived one Gen 0 collection. It acts as a short buffer, so objects that live slightly longer do not immediately reach Gen 2. - Gen 2 — objects that survived Gen 1. Contains long-lived data. Collected least often; a Gen 2 collection is a full GC and the most expensive. Promotion When a collection of a generation runs, the surviving objects move to the next older generation (0 to 1, 1 to 2). Gen 2 survivors stay in Gen 2. Collection rules - Collecting Gen 1 also collects Gen 0; collecting Gen 2 collects everything. - Write barriers and a "card table" track references from older generations to younger objects, so the GC can collect Gen 0 without scanning the whole Gen 2. Special heaps - Large Object Heap: objects of 85 KB or more, collected together with Gen 2. - Pinned Object Heap (.NET 5+): for objects that must not move, reducing fragmentation caused by pinning. Practical implications - Allocate less in hot paths to reduce Gen 0 collections. - Avoid keeping temporary objects alive just long enough to reach Gen 2. - Watch metrics: a healthy service shows many Gen 0, fewer Gen 1, and rare Gen 2 collections. A steadily growing Gen 2 heap suggests a memory leak. You can check an object's generation with GC.GetGeneration(obj), and see collection counts with GC.CollectionCount(gen) or dotnet-counters.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics