The .NET garbage collector reclaims managed objects that are no longer reachable. New objects normally begin in generation 0; survivors can be promoted to generations 1 and 2. Large objects use the large object heap and are collected with Gen 2. Example: long before = GC.GetAllocatedBytesForCurrentThread(); var buffer = new byte[4096]; long bytes = GC.GetAllocatedBytesForCurrentThread() - before; GC does not provide timely release of external resources. Use using or await using for streams and database connections. Do not call GC.Collect in normal code; measure allocation rate, heap size, pauses, and retained objects with counters, traces, or dumps before optimizing.
Backbencher Developer · · 0 likes