Garbage collection and roots
Garbage collection and roots
Garbage collection reclaims managed objects that are no longer reachable through strong references from GC roots. Roots include live local references, static fields, and runtime handles. A cycle of objects does not keep itself alive when no root can reach it. Collection is scheduled by the runtime; losing the last reference does not mean that memory is immediately reclaimed. Dispose is a separate resource-lifetime contract.
Proposed baseline: .NET 10 / C# 14 (SDK 10.0.404 inherited). Ordinary CoreCLR SDK console build, not Native AOT. No additional packages. Outputs are reasoned, not executed.
Trace a shared object from a root
Two local variables can reach one object. Clearing one reference does not remove the other path.
using System;Node? left = new Node { Name = "Catalog" };Node right = left;left = null;Console.WriteLine(left is null);Console.WriteLine(right.Name);public class Node { public string Name { get; set; } = ""; }The remaining strong reference is sufficient for access. This illustrates reachability; it does not force a collection.
Expected observation (reasoned, not executed):
TrueCatalogModel a cycle without claiming GC timing
Create two linked nodes, then sever the external path. The diagram is root -> first -> second -> first before the assignment.
using System;Node? root = new Node();root.Next = new Node { Next = root };Console.WriteLine(ReferenceEquals(root, root.Next.Next));root = null;Console.WriteLine(root is null);public class Node { public Node? Next { get; set; } }The cycle is not a root. If no other live root reaches it, it is eligible for collection. The printed values do not prove that the collector ran.
Expected observation (reasoned, not executed):
TrueTrueDispose while the object remains reachable
Use an explicit scope for a disposable toy resource. Keep a reference afterward to separate resource cleanup from object lifetime.
using System;var ticket = new Ticket();using (ticket) { Console.WriteLine(ticket.Closed); }Console.WriteLine(ticket.Closed);GC.KeepAlive(ticket);public sealed class Ticket : IDisposable{ public bool Closed { get; private set; } public void Dispose() => Closed = true;}The using statement invokes Dispose even though ticket remains accessible. KeepAlive expresses a lifetime boundary; it does not dispose the object.
Expected observation (reasoned, not executed):
FalseTrueCommon mistakes and edge cases
A static cache or event publisher can retain objects unintentionally. Remove unused references or subscriptions according to their ownership contracts. Avoid forced GC as routine cleanup. Finalization, if present, is not a predictable replacement for Dispose; use using or await using for deterministic cleanup. Lexical scope is not the same as actual GC liveness: the JIT may consider a local dead before scope exit.
Practice and recap
Draw a graph with a static cache pointing to A, A pointing to B, and B pointing to A. Explain what happens when the cache removes A. Then explain how a file handle in A should close.
Hint: Reachability starts from roots, not cycles.
Learning objective: Explain reachability without equating GC with disposal. See accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Garbage collection and roots — knowledge check
Apply the lesson concepts to the following scenarios.
What makes a managed object reachable?
Can an unreachable cycle be collected?
What does setting a reference to null guarantee?
What does using normally arrange?
What can GC.KeepAlive express?