← All blogs

FullStack Engineering CourseLesson 2 of 20 44 min

Memory Management and Garbage Collection in .NET

The stack and the heap

A .NET program uses two kinds of memory. Each thread has a stack: a small, fixed block (1 MB by default on Windows) that holds a method's local variables and parameters while it runs. Using the stack costs almost nothing, and the memory is freed the moment the method returns. The managed heap is one large area shared by all threads. Objects live there as long as something refers to them. Allocating on the heap is also cheap, but freeing it is the garbage collector's (GC's) job, and that is where the cost is.

Where a value lives depends on its type and where it is declared:

  • Reference types (class, record, string, arrays, delegates) always live on the heap, and a variable holds a reference to them.
  • Value types (int, double, bool, DateTime, enums, struct, record struct) live wherever they are declared: as a local variable, on the stack; as a field of a class, inside that object on the heap; as an array element, inside the array on the heap.
C#
public struct Point { public int X, Y; }          // value type
public class Shape { public Point Origin; }        // reference type with a struct field

void Draw()
{
    int count = 3;                 // stack
    Point p = new() { X = 1 };     // stack: the whole struct is the local variable
    var shape = new Shape();       // the reference is on the stack, the Shape object on the heap,
                                   // and shape.Origin lives inside that heap object
    var points = new Point[100];   // one heap object holding 100 Points inline (no 100 allocations)

    Point copy = p;                // copies the struct: changing copy.X leaves p.X alone
    Shape alias = shape;           // copies the reference: both variables see the same object
}

Some local variables end up on the heap even though they look like stack variables. A local captured by a lambda moves into a closure (a compiler-generated object) on the heap. The locals of an async method that are needed after an await become fields of its state machine, which moves to the heap the first time the method has to wait (lesson 1 showed this state machine). Iterator methods (yield return) work the same way.

Boxing and other hidden allocations

Boxing happens when a value type is used where an object or an interface is expected: the runtime allocates a heap object and copies the value into it. Unboxing copies it back. One boxing operation costs almost nothing, but inside code that runs millions of times (a hot path), the allocations pile up and slow the program down.

C#
int id = 42;
object boxed = id;                       // box: allocates
int back = (int)boxed;                   // unbox: copies the value out

var list = new ArrayList { 1, 2, 3 };    // every int boxed (legacy non-generic collection)
var good = new List<int> { 1, 2, 3 };    // generics: stored inline, no boxing

string s1 = string.Format("{0} items", id);   // id boxed to fit the object parameter
string s2 = $"{id} items";                    // .NET 6+ interpolation handlers: no boxing

IComparable c1 = id;                     // casting a struct to an interface boxes it
void Log<T>(T value) where T : IComparable<T> { /* no boxing: generic constraint */ }

Learn to recognise the common hidden allocations, because they are what a memory profiler shows you:

  • closures that capture local variables
  • LINQ chains, which allocate enumerators and delegates
  • string concatenation in loops
  • params arrays
  • boxing, through object, non-generic collections or interfaces
  • async methods that actually wait
  • ToList() or ToArray() calls made just to loop once
C#
// Allocates a closure (captures threshold) and a delegate on every call.
public int CountLarge(List<Claim> claims, decimal threshold) =>
    claims.Count(c => c.Amount > threshold);

// Allocation-free version for a hot path: a plain loop.
public int CountLargeFast(List<Claim> claims, decimal threshold)
{
    int count = 0;
    foreach (var c in claims)            // List<T>'s struct enumerator: no allocation
        if (c.Amount > threshold) count++;
    return count;
}

// A static lambda cannot capture anything, so the compiler caches one delegate.
static readonly Func<Claim, bool> IsRejected = static c => c.Status == ClaimStatus.Rejected;

This does not mean LINQ is bad. Write clear code first and then only optimise the hot paths pointed by the profiler such as a request handler called thousands of times a second, or a parser running over millions of rows. An allocation in code that runs once at startup does not matter.

How the garbage collector works

The garbage collector finds the objects that are still in use and frees everything else. A collection works in three steps:

  1. Mark: starting from the roots (static fields, local variables and parameters on every thread's stack, CPU registers and GC handles), it follows every reference and marks each object it reaches.
  2. Sweep: anything not marked is garbage.
  3. Compact: it usually moves the live objects together so free memory is one block again, and updates every reference to the moved objects.

While it marks and compacts, it has to pause the application's threads, at least for part of the time.

Marking the whole heap every time would be slow, so the heap is split into generations. This works because most objects die young (a request's DTOs, a LINQ enumerator, a temporary string), while objects that survive for a while usually live a long time (caches, singletons, configuration).

GenerationWhat lives thereHow often it is collected
Gen 0Newly allocated small objectsVery often, and cheaply: the area is small and most of it is garbage
Gen 1Objects that survived one gen 0 collection; a buffer between short and long livedLess often
Gen 2Long-lived objects: caches, singletons, static dataRarely; a gen 2 (full) collection also collects gen 0 and gen 1 and is the expensive one
LOH (Large Object Heap)Objects of 85,000 bytes or more, mostly large arraysOnly together with gen 2; not compacted by default
POH (Pinned Object Heap, .NET 5+)Objects allocated as pinned, such as I/O buffers handed to native codeOnly together with gen 2; never moved

An object that survives a collection is promoted (moved up) to the next generation. A gen 0 collection only checks gen 0. But some gen 0 objects are referenced by older objects, and the runtime must find them without scanning gen 2. For this, it keeps a card table (a record of the recently changed parts of the older generations). You can watch promotion happen:

C#
var order = new object();
Console.WriteLine($"New object:          gen {GC.GetGeneration(order)}");
GC.Collect();
Console.WriteLine($"After one collection: gen {GC.GetGeneration(order)}");
GC.Collect();
Console.WriteLine($"After two:            gen {GC.GetGeneration(order)}");

var big = new byte[85_000];             // 85,000 bytes plus the array header: goes to the LOH
Console.WriteLine($"85 KB array:          gen {GC.GetGeneration(big)}  (the LOH reports as gen 2)");
Output
New object:          gen 0
After one collection: gen 1
After two:            gen 2
85 KB array:          gen 2  (the LOH reports as gen 2)

GC.Collect() is fine for demos like this one. In application code it is almost always a mistake. It forces an expensive full collection and promotes every live object to an older generation, where it costs more to collect later. The GC already decides when to collect, based on how much has been allocated.

The Large Object Heap

Moving a large array during compaction is expensive, so objects of 85,000 bytes or more go to a separate Large Object Heap (LOH). The LOH is collected only together with gen 2, and by default it is not compacted. This causes two problems:

  • A service that allocates many short-lived large buffers (reading whole request bodies into byte arrays, building big strings, a List<T> growing past about 10,000 items) triggers expensive gen 2 collections for objects that die young.
  • Because the LOH is not compacted, gaps between live large objects can fragment it: memory use grows even though little of it is in use.
C#
// Allocates a new large array for every upload: LOH churn and gen 2 collections.
byte[] body = new byte[request.ContentLength ?? 0];
await request.Body.ReadExactlyAsync(body, ct);

// Better: stream it, so no buffer ever needs to hold the whole body.
await using var file = File.Create(path);
await request.Body.CopyToAsync(file, ct);

// If you really need a large buffer, rent it (ArrayPool, below) instead of allocating it.

// Compact the LOH once, on the next full collection, if fragmentation is proven to be a problem:
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;

GC modes: workstation, server and background

There are two kinds of GC:

  • Workstation GC uses one heap and suits desktop apps: short pauses and low memory use.
  • Server GC creates one heap and one GC thread per CPU core and collects in parallel. It gives much higher throughput for services but uses more memory. ASP.NET Core apps use Server GC by default.

Background GC (also called concurrent GC) is on by default in both. It runs most of a gen 2 collection alongside the application, so a full collection does not stop every thread for its whole length.

HTML
<!-- In the .csproj (or the equivalent settings in runtimeconfig.json) -->
<PropertyGroup>
  <ServerGarbageCollection>true</ServerGarbageCollection>      <!-- default for ASP.NET Core -->
  <ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
</PropertyGroup>

Containers change things. The GC reads the container's memory limit and, by default, keeps its heap below 75% of it. Server GC in a container with many cores but little memory can hit that limit or collect constantly. To fix this, .NET 8 added DATAS (Dynamic Adaptation To Application Sizes), and .NET 9 turned it on by default for Server GC. DATAS starts with a few heaps and adds more as load grows. If a containerised service uses far more memory than expected, check its GC mode and whether its memory limit is realistic before tuning anything else.

IDisposable, finalisers and unmanaged resources

The GC manages memory and nothing else. A file handle, a socket, a database connection or an operating system lock is an unmanaged resource. The GC knows nothing about it. If you wait for the GC to clean up the object that owns it, the resource stays open for an unknown time. IDisposable is the contract for releasing such resources at a known moment: Dispose releases them now, and a using statement makes sure Dispose is called even when an exception is thrown.

C#
// using declaration: disposed at the end of the enclosing scope, in reverse order.
using var connection = new SqlConnection(connectionString);
await connection.OpenAsync(ct);
using var command = new SqlCommand("SELECT COUNT(*) FROM Claims", connection);
var total = (int)(await command.ExecuteScalarAsync(ct))!;

// IAsyncDisposable: for resources whose cleanup does I/O, such as flushing a stream.
await using var writer = new StreamWriter(File.Create("claims.csv"));
await writer.WriteLineAsync("id,amount");

A finaliser (~ClassName) is a safety net for code that forgot to call Dispose. The GC runs it on a separate thread some time after the object becomes unreachable. Finalisers are expensive: an object with one survives at least one extra collection (it waits in the finalisation queue, the finaliser runs, and only the next collection frees it), so it usually gets promoted to an older generation. Write a finaliser only if your class owns a raw unmanaged handle, and in modern code not even then: wrap the handle in a SafeHandle, which already has a correct finaliser, and give your class a plain Dispose.

C#
// The classic Dispose pattern, for an unsealed class that owns both kinds of resource.
public class ReportExporter : IDisposable
{
    private readonly FileStream _stream;          // managed object that owns an OS handle
    private readonly SafeFileHandle _lockHandle;  // SafeHandle: brings its own finaliser
    private bool _disposed;

    public ReportExporter(string path)
    {
        // File.OpenHandle (.NET 6+) returns a SafeFileHandle. FileShare.None means no other
        // process can open the .lock file while we hold it, so it works as a cross-process lock.
        _lockHandle = File.OpenHandle(path + ".lock", FileMode.OpenOrCreate,
                                      FileAccess.ReadWrite, FileShare.None);
        _stream = new FileStream(path, FileMode.Create);
    }

    public void Dispose()
    {
        Dispose(disposing: true);
        GC.SuppressFinalize(this);   // a subclass may add a finaliser; no need to run it now
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed) return;       // Dispose must be safe to call more than once
        if (disposing)
        {
            _stream.Dispose();       // only touch other managed objects when called from Dispose
            _lockHandle.Dispose();   // releases the lock file
        }
        _disposed = true;
    }
}

// A sealed class with only managed disposable fields needs none of that:
public sealed class ClaimFileReader(Stream stream) : IDisposable
{
    public void Dispose() => stream.Dispose();
}

Two disposal mistakes are common in services:

  • Creating and disposing an HttpClient per request leaves sockets open in TIME_WAIT and can run out of them under load. Use IHttpClientFactory, or one long-lived HttpClient with a SocketsHttpHandler that recycles connections (PooledConnectionLifetime) so DNS changes are picked up.
  • Disposing something you did not create. The DI container disposes the objects it created at the end of their scope.

Memory leaks in managed code

The GC never leaks an object that is not referenced by anything. So a .NET memory leak is always an object that is still referenced but no longer needed. The usual causes:

CauseWhat keeps the objects aliveFix
Event subscriptionsA long-lived publisher's event holds a reference to every subscriberUnsubscribe in Dispose, or use weak events
Static collections and cachesA static Dictionary or List that only ever growsBound it: IMemoryCache with size limits and expiry
TimersA running System.Threading.Timer keeps its callback's target aliveDispose the timer when the owner is disposed
Captive dependenciesA Singleton holding a Scoped or Transient objectCreate a scope per unit of work (IServiceScopeFactory)
ClosuresA long-lived delegate captures a large objectCapture only what is needed; use static lambdas
Undisposed resourcesStreams, connections, CancellationTokenSource registrationsusing / await using
C#
public sealed class ClaimDashboard : IDisposable
{
    private readonly ClaimEventBus _bus;          // a Singleton that lives for the whole app

    public ClaimDashboard(ClaimEventBus bus)
    {
        _bus = bus;
        _bus.ClaimUpdated += OnClaimUpdated;      // the bus now holds a reference to this dashboard
    }

    private void OnClaimUpdated(object? sender, ClaimUpdatedEventArgs e) { /* refresh */ }

    // Without this, every dashboard ever created stays in memory for the app's lifetime.
    public void Dispose() => _bus.ClaimUpdated -= OnClaimUpdated;
}

When the subscriber should not control the lifetime at all (for example, a cache keyed by objects owned by someone else), use WeakReference<T> or ConditionalWeakTable<TKey,TValue>. They hold a reference that does not keep the target alive.

Span<T> and Memory<T>: working without allocating

Span<T> is a view over a continuous block of memory: part of an array, a string, a stackalloc buffer or native memory. Slicing a span creates a new view, not a copy, so parsing and formatting code can work on parts of a buffer without allocating substrings or new arrays. Modern .NET parses text with ReadOnlySpan<char>, and most built-in APIs (int.Parse, Utf8Parser, Encoding, Stream.Read) accept spans.

C#
// Sums "10,20,30" without allocating a single string or array.
static int SumCsv(ReadOnlySpan<char> line)
{
    int sum = 0;
    while (!line.IsEmpty)
    {
        int comma = line.IndexOf(',');
        ReadOnlySpan<char> field = comma < 0 ? line : line[..comma];   // a view, not a copy
        sum += int.Parse(field);
        line = comma < 0 ? ReadOnlySpan<char>.Empty : line[(comma + 1)..];
    }
    return sum;
}

Console.WriteLine(SumCsv("10,20,30"));        // string converts implicitly to ReadOnlySpan<char>

// stackalloc: a small temporary buffer on the stack instead of the heap.
Span<byte> hash = stackalloc byte[32];
SHA256.HashData("claim-42"u8, hash);          // "..."u8 is a UTF-8 literal, also no allocation
Console.WriteLine(Convert.ToHexString(hash)[..16]);
Output
60
54EA521E4AD756C1

Span<T> is a ref struct, a type that can only live on the stack. That is what lets it safely point at stack memory. It cannot be a field of a class, be boxed, be captured by a lambda or be kept across an await. (C# 13 allows ref struct locals in async methods, but still not across an await.) Memory<T> is the heap-friendly version: a normal struct that you can store in fields and use in async code. Call memory.Span when you need to work with the data. Keep stackalloc small (a few hundred bytes to a few KB), because a large one can overflow the stack. For anything bigger, rent from a pool.

Storing a Memory<T> in a class field does not create a separate heap allocation. Memory<T> is a small struct (a reference to the underlying array, a start index and a length). Like any struct field, it is stored inside the class object on the heap. The data it points to is separate: usually an array that already lives on the heap. Slicing a Memory<T> allocates nothing.

C#
public sealed class UploadBuffer
{
    private readonly byte[] _array = new byte[4096];   // one heap allocation: the array
    private Memory<byte> _filled;                      // stored inside this object; no allocation of its own

    public void SetFilled(int count) => _filled = _array.AsMemory(0, count);  // slicing allocates nothing
}

This is also why Span<T> cannot be a field. A span may point at stack memory. If a heap object kept that span, the span could outlive the stack frame and point at memory that has been reused. Memory<T> never points at stack memory, so it is safe to keep.

Span<T> / ReadOnlySpan<T>Memory<T> / ReadOnlyMemory<T>
Kindref struct, stack onlyOrdinary struct
Can point atArrays, strings, stackalloc, native memoryArrays, strings, pooled memory (not stackalloc)
Field of a class, in a lambda, across awaitNoYes
Use it forSynchronous parsing and formattingAsync I/O and storing a slice for later

ArrayPool and object pooling

When code needs a temporary buffer that is too big for the stack, rent one from ArrayPool<T>.Shared and return it afterwards. This avoids a new allocation each time, and for buffers over 85,000 bytes it keeps them off the LOH. Rules:

  • The pool may give you an array larger than the requested size, so always use the requested length, not buffer.Length.
  • After you return an array, it belongs to the pool: never use it again.
  • Clear it on return if it held sensitive data.
C#
public static async Task CopyAsync(Stream source, Stream destination, CancellationToken ct)
{
    byte[] buffer = ArrayPool<byte>.Shared.Rent(81_920);      // may be larger than 81,920
    try
    {
        int read;
        while ((read = await source.ReadAsync(buffer.AsMemory(0, 81_920), ct)) > 0)
            await destination.WriteAsync(buffer.AsMemory(0, read), ct);   // Memory<T>: fine across await
    }
    finally
    {
        ArrayPool<byte>.Shared.Return(buffer, clearArray: false);  // true if it held patient data
    }
}

The same idea works for other expensive objects. Microsoft.Extensions.ObjectPool provides ObjectPool<T>, often used for StringBuilder, and Microsoft.IO.RecyclableMemoryStream replaces MemoryStream in services that create many streams. Pooling only pays off when the objects are large or expensive to create and the code is a hot path. Otherwise it adds complexity and the risk of using an object after returning it.

How ArrayPool works inside

ArrayPool<T>.Shared is a static singleton. A static field refers to it, so it is a GC root and lives for the whole application. The arrays inside it are ordinary heap arrays. The pool keeps them in buckets by size: 16, 32, 64 elements and so on, doubling each time. When you ask for 5,000 elements, you get an array from the 8,192 bucket. That is why Rent can return a bigger array than you asked for. Each bucket has a small per-thread cache plus per-CPU-core stacks, so Rent and Return rarely need a lock.

When you call Return, the array goes back into the pool. Nothing is freed or copied:

  1. Clearing (optional): Return(array, clearArray: true) wipes the contents first. With the default (false), your old data stays in the array and the next renter can see it.
  2. Size check: the pool finds the bucket for the array's length. If the length does not match a bucket (the array did not come from this pool), the shared pool throws an ArgumentException. Arrays bigger than the pool's maximum size are simply dropped.
  3. Storage: the array goes into the per-thread slot for its size, so the next Rent on that thread gets it back with no locking and no allocation. If that slot is full, it goes onto a per-core stack. Each stack holds only a few arrays (8 per size per core in current .NET). If the stack is also full, the pool drops the array and it becomes normal garbage.
  4. Ownership: from this moment the array belongs to the pool. Another part of the program may rent it straight away.
C#
byte[] first = ArrayPool<byte>.Shared.Rent(1000);   // gets a 1,024-byte array
ArrayPool<byte>.Shared.Return(first);

byte[] second = ArrayPool<byte>.Shared.Rent(1000);  // same thread, same size bucket
Console.WriteLine(ReferenceEquals(first, second));  // the same array came back
Output
True
(usually; the pool does not promise it)

Two bugs to avoid:

  • Using an array after returning it. Another request may already be writing into it, so you read its data or overwrite it (a use-after-return bug).
  • Returning the same array twice. The pool may then give that one array to two callers, who both write into it at the same time.

Does the GC collect pooled arrays? Not while they are in the pool, because the pool refers to them. That is the point of pooling. But the shared pool does not keep them forever. It watches for gen 2 collections and memory pressure, and drops arrays that have not been used for a while (this is called trimming). The GC can then collect them. Under heavy memory pressure, it trims more aggressively.

If you forget to call Return, nothing leaks. The array becomes ordinary garbage once your code stops using it. But you lose the benefit of pooling, and for large arrays this hurts:

  • An array of 85,000 bytes or more is allocated straight onto the LOH, which counts as gen 2. GC.GetGeneration returns 2 from the moment it is allocated.
  • After your code drops it, it stays in memory until the next gen 2 (full) collection, because the LOH is only collected then.
  • Its space becomes a gap in the LOH, which is not compacted by default. This is how fragmentation builds up.
  • Every Rent now allocates a new LOH array. Large allocations count heavily toward the GC's gen 2 budget (how much can be allocated before a full collection starts), so full collections happen more often.

Rounding up to the bucket size can also push a request onto the LOH:

You requestThe pool gives youWhere a new array goes
50,000 bytes65,536 bytesSmall object heap (gen 0)
70,000 bytes131,072 bytesLOH (gen 2)

So always return what you rent, in a finally block. Custom pools made with ArrayPool<T>.Create live only as long as you keep a reference to them. When the pool becomes unreachable, it is collected together with all its arrays.

Strings

Strings are immutable (they cannot be changed after creation), so every concatenation, Substring, ToUpper or Replace creates a new string. To keep allocations down:

  • In loops, use StringBuilder, or string.Join and string.Concat when you know all the parts.
  • In .NET 6 and later, interpolated strings ($"...") write into a pooled buffer and do not box value types, so they are usually the cheapest readable option.
  • Compare strings with an explicit StringComparison (Ordinal or OrdinalIgnoreCase for identifiers) instead of calling ToLower on both, which creates two new strings.
  • For text you only read, use ReadOnlySpan<char> instead of Substring.
C#
// Allocates a new string on every iteration.
string csv = "";
foreach (var claim in claims) csv += $"{claim.Id},{claim.Amount}\n";

// One growing buffer.
var sb = new StringBuilder(capacity: claims.Count * 16);
foreach (var claim in claims) sb.Append(claim.Id).Append(',').Append(claim.Amount).Append('\n');
string csv2 = sb.ToString();

// Case-insensitive comparison without allocating.
bool isAdmin = role.Equals("admin", StringComparison.OrdinalIgnoreCase);

Structs, classes and records

A struct avoids a heap allocation per instance, and an array of structs stores them inline. But a struct is copied on every assignment and every time it is passed by value. That is cheap for small structs and expensive for big ones. Use a struct for small, immutable values that represent one thing (a money amount, a coordinate, a date range). Mark it readonly so the compiler can avoid hidden copies, and pass larger ones by reference with in. Use a class for anything with identity, shared state that changes, or more than a few fields.

class / recordstruct / record struct
StoredOn the heap; variables hold referencesInline: on the stack, or inside the containing object or array
Assignment copiesThe referenceThe whole value
Allocation per instanceYesNo (unless boxed)
Good forEntities, services, anything large or sharedSmall immutable values, up to about 16 bytes as a rule of thumb
PitfallsGC pressure in hot pathsCopy cost when large, boxing through interfaces, mutable structs behaving surprisingly

A record is a type the compiler fills in for you. It generates value equality (two records are equal when all their values are equal), a readable ToString, a Deconstruct method and support for with expressions. There are two kinds, and they behave very differently in memory:

  • record (also written record class) is a class. Every instance is a heap allocation, like any other class.
  • record struct is a struct. It needs no heap allocation, but it is copied on every assignment, like any other struct.
C#
public record Money(decimal Amount, string Currency);            // record class: lives on the heap
public readonly record struct Coordinate(double Lat, double Lon);  // record struct: no heap allocation

var a = new Money(100m, "INR");
var b = new Money(100m, "INR");

Console.WriteLine(a == b);                 // compares values, not references
Console.WriteLine(ReferenceEquals(a, b));  // still two separate objects on the heap

var c = a with { Amount = 120m };          // 'with' creates a new copy: another allocation
Console.WriteLine(a);                      // the original is unchanged
Output
True
False
Money { Amount = 100, Currency = INR }

Things to know about records and memory:

  • A with expression never changes the original. It creates a copy with the changed values. For a record class, every with is a new heap allocation, so calling it in a loop over millions of items creates the same churn as any other allocation.
  • A positional record struct is mutable by default: its properties have get and set. Write readonly record struct to make them init-only, so the compiler can avoid hidden copies.
  • Generated equality compares every field. For a record with many fields, Equals and GetHashCode do more work than a reference comparison. This matters when records are dictionary keys or are compared in hot paths.
  • Equality is shallow. A record with a List<T> property compares the list by reference, not by its contents, so two records holding equal lists are not equal.
  • Do not change a record used as a dictionary or HashSet key. Its hash code depends on its values, so the lookup can no longer find it.

Use records for data that is defined by its values: DTOs (data transfer objects), messages and events, and value objects such as Money. Use a record struct for small values on hot paths. Avoid records for EF Core entities: an entity has an identity (its ID), and value equality conflicts with how change tracking works.

Measuring: BenchmarkDotNet and the diagnostic tools

Do not guess where memory goes; measure it. BenchmarkDotNet runs a method many times in a separate process after warming it up. With [MemoryDiagnoser], it reports how much each call allocates and how many gen 0, 1 and 2 collections that caused. Always run it in Release mode.

C#
// dotnet add package BenchmarkDotNet ; dotnet run -c Release
[MemoryDiagnoser]
public class ParseBenchmarks
{
    private const string Line = "10,20,30,40,50,60,70,80";

    [Benchmark(Baseline = true)]
    public int WithSplit() => Line.Split(',').Sum(s => int.Parse(s));   // allocates an array and 8 strings

    [Benchmark]
    public int WithSpan() => SumCsv(Line);                               // allocates nothing

    private static int SumCsv(ReadOnlySpan<char> line)
    {
        int sum = 0;
        while (!line.IsEmpty)
        {
            int comma = line.IndexOf(',');
            sum += int.Parse(comma < 0 ? line : line[..comma]);
            line = comma < 0 ? ReadOnlySpan<char>.Empty : line[(comma + 1)..];
        }
        return sum;
    }
}

BenchmarkRunner.Run<ParseBenchmarks>();

The results table shows Mean (time per call), Gen0 (collections per 1,000 calls) and Allocated (bytes per call). Here, WithSpan shows a dash in the Allocated column, meaning zero bytes, while WithSplit allocates an array and eight substrings on every call. Multiply that difference by the number of calls per second, and you have the GC work you save.

In a running service, use the .NET diagnostic tools in this order (install them with dotnet tool install -g):

Shell
# 1. Is the GC the problem? Live counters: heap size, allocation rate, % time in GC, collections per generation.
dotnet-counters monitor --process-id 12345 --counters System.Runtime

# 2. What is on the heap? A GC heap dump (small, safe in production); open the .gcdump in Visual Studio or PerfView.
dotnet-gcdump collect --process-id 12345

# 3. Who allocates? A trace of allocation events and CPU samples; open in PerfView, Visual Studio or speedscope.
dotnet-trace collect --process-id 12345 --profile gc-verbose

# 4. A full memory dump for deep analysis (large: contains every byte of the heap, including data).
dotnet-dump collect --process-id 12345

To find a leak:

  1. Take two heap snapshots some time apart while the service is under steady load.
  2. Compare them. The types whose count keeps growing are the leak.
  3. Follow the tool's retention path (also called path to root) to see which reference keeps them alive, such as a static dictionary or an event on a Singleton.

JetBrains dotMemory and the Visual Studio memory profiler do this comparison for you. If instead you see a high % time in GC with a high allocation rate, the problem is too many short-lived allocations, and the trace shows which methods allocate the most.

Check your understanding

QuestionShort answer
Where do value types live?Where they are declared: a local on the stack, a field inside its object on the heap, an array element inside the array. Not always on the stack.
What is boxing and why avoid it?Wrapping a value type in a heap object to use it as object or an interface. Each box is an allocation; generics avoid it.
Why does the GC use generations?Most objects die young, so collecting only gen 0 often is cheap; long-lived objects in gen 2 are scanned rarely.
What is the LOH?The heap for objects of 85,000 bytes or more; collected only with gen 2 and not compacted by default, so it can fragment.
Workstation or Server GC?Server GC (default in ASP.NET Core) uses a heap per core for throughput; Workstation uses less memory. .NET 9 DATAS adapts Server GC to container sizes.
Dispose or finaliser?Dispose releases resources deterministically; a finaliser is a slow safety net that delays collection. Prefer SafeHandle over writing finalisers.
Why call GC.SuppressFinalize?Dispose has already cleaned up, so the object can skip the finalisation queue and be collected in one pass.
How can managed code leak?Something still references objects no longer needed: events, static caches, timers, captive dependencies.
Span<T> or Memory<T>?Span for synchronous code (stack-only ref struct, no await); Memory when you need to store it or use it across await.
What does ArrayPool solve?Repeated large temporary buffers: rent and return instead of allocating, keeping them off the LOH.
What happens when you return an array to ArrayPool?It goes back into a per-thread slot or per-core stack for its size (cleared only if you ask). The next Rent reuses it; if the pool is full, the array is dropped and becomes garbage.
What if you never return a large rented array?Nothing leaks, but the array (on the LOH, gen 2) waits for a full collection, and every Rent allocates a new LOH array, causing more gen 2 collections.
Does a Memory<T> field allocate?No. The struct is stored inside the object; it points at an array that already exists. Slicing it allocates nothing.
Should you call GC.Collect()?Almost never; it forces a full collection and promotes live objects, making later collections costlier.
How do you find a memory leak?Compare two heap snapshots under load, find types that keep growing, and follow their retention path to the root.
record or record struct?A record class is a heap object with value equality; a record struct avoids the allocation but is copied. Use readonly record struct for small values on hot paths.
Does a with expression change the record?No. It creates a new copy with the changed values; for a record class that is a new heap allocation.

Summary

  • Reference types live on the heap. Value types live wherever they are declared, and captured locals and async state move to the heap.
  • The GC marks what is reachable from the roots and compacts away the rest. It collects gen 0 often and gen 2 rarely, and keeps large objects on a separate heap that is not compacted.
  • Keep allocations low on hot paths: avoid boxing, closures and repeated string building, parse with Span<T>, rent large buffers from ArrayPool (and always return them), and stream data instead of loading whole payloads.
  • Release unmanaged resources with Dispose and using, and prefer SafeHandle to writing finalisers.
  • Treat leaks as references you forgot to drop: events, static caches, timers and captive dependencies.
  • Use records for value-like data. A record class is still a heap allocation, and every with expression creates a copy; use readonly record struct for small values on hot paths.
  • Measure with BenchmarkDotNet, dotnet-counters, dotnet-gcdump and dotnet-trace before changing anything.

The next lesson moves from how code runs to how it is structured: SOLID, DRY, KISS and YAGNI, and dependency injection in ASP.NET Core.

Found this useful? Share it: LinkedIn X

Comments