Caffeinated Coder
Book an appointment
Lesson 19 · Advanced .NET

Memory and the garbage collector

In C#, locals live in a method's stack frame and every object made with new lives on the managed heap, which the CLR owns. Follow a seven-line program as it allocates two objects in generation 0, drops one reference, and forces a gen 0 collection: the garbage collector marks what the roots reach, reclaims the rest, compacts and promotes the survivor to gen 1, and updates the reference to it. GC.GetGeneration then prints 1.

14 min10 steps5 questionsAdvanced
Fig 19 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.