liveBook · Manning

Generation 2

As explained in .NET in Action, Second Edition

Generation 2 is the oldest .NET garbage-collection generation for objects that have survived earlier collections and are generally long-lived.

Generation 2 (Gen2) holds heap objects that survived both Gen0 and Gen1 collections. An object remains alive while it is referenced; objects that survive Gen1 move to Gen2 and are generally considered long-lived. section 10.3.4

Gen2 collections are infrequent. Keeping references after they are needed can leave objects in Gen2. Because the garbage collector may expand the heap rather than compact it, many lingering Gen2 objects can increase memory use. When an application spends too much time collecting garbage or uses unexpectedly high memory, inspect collection counts and memory profiles. section 10.3.4

Table 10.2 GCStats table from PerfView from the Newtonsoft benchmark method profile (view table figure)

Gen

Count

Total pause

Total alloc

ALL

2489

1,033.2 ms

9,712.2 MB

0

1913

769.5 ms

8,023.4 MB

1

382

142.8 ms

1602.3 MB

2

194

120.9 ms

86.5 MB

The GCStats table shows collection counts, pause time and allocation by generation. Its 194 Gen2 collections were artificially induced by BenchmarkDotNet, so do not treat that count as a normal application rate. section 10.3.4

How .NET in Action, Second Edition covers it

How deep it goes
Explains promotion through generations, infrequent Gen2 collections and heap-growth implications without detailing collector algorithms.
Examples
Uses PerfView GCStats from a Newtonsoft benchmark, noting that BenchmarkDotNet artificially induced its 194 Gen2 collections.
What you can do after
Diagnose/debug with Gen2 collection statistics and memory profiles to investigate retained objects and heap growth.
How it connects
Connects Gen2 to object references, heap allocation, compaction, memory profiling and garbage-collection statistics.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.