wasAsked.

Garbage Collection

HCA Healthcare · Role not specified · technical

Published

Year not specified · C#

Community answers

Posted by wasAsked editorial collection

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

Garbage Collection (GC) is the automatic memory management system of the .NET runtime (CLR). It allocates memory for objects on the managed heap and frees it when objects are no longer used, so developers do not manually release memory. Allocation - Reference-type objects are allocated on the managed heap. Allocation is fast: the runtime moves a pointer forward in a contiguous region. - Value types live inline (on the stack for locals, or inside their containing object). When the GC runs - When the Gen 0 budget fills up (most common), when the OS signals low memory, or when GC.Collect() is called. How it decides what to free 1. Mark: starting from roots (static fields, local variables and parameters on thread stacks, CPU registers, GC handles), it finds every object that is still reachable. 2. Sweep or reclaim: memory used by unreachable objects becomes free. 3. Compact: survivors are moved together to remove gaps, and references are updated, keeping allocation fast. Generations make it efficient - Gen 0 for new objects (collected often), Gen 1 as a buffer, Gen 2 for long-lived objects (collected rarely). Most objects die young, so most collections are small and quick. - Objects of 85,000 bytes or more go to the Large Object Heap, which is collected with Gen 2. GC modes - Workstation GC for desktop apps; Server GC (default in ASP.NET Core) uses multiple heaps for throughput. - Background (concurrent) GC reduces pauses for Gen 2 collections. What the GC does not handle - Unmanaged resources such as file handles, network sockets, database connections and native memory. Release them with IDisposable and using: await using var connection = new SqlConnection(connString); - Finalizers (~ClassName) are a last-resort cleanup for unmanaged resources and slow down collection; prefer SafeHandle and the Dispose pattern. Avoiding memory problems - Unsubscribe event handlers, bound caches with size limits, avoid static collections that grow forever, reduce allocations in hot paths (pooling, Span<T>), and do not call GC.Collect() in production code without strong evidence. Monitoring tools: dotnet-counters, dotnet-gcdump, Visual Studio diagnostics and PerfView.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics