Phase 24 of 30 · Topic 24.5

Memory Profiling with `dotnet-dump`, `dotnet-gcdump` & PerfView

1Concept

Diagnosing production memory leaks in Linux Docker containers uses CLI diagnostics tools: `dotnet-gcdump` captures lightweight GC heap graphs, and `dotnet-dump` analyzes full core dumps with SOS commands (`!dumpheap -stat`).

2Architecture Diagram

Live Linux Container
       │
dotnet-gcdump collect -p 1
       │
Emits .gcdump file ──> Open in Visual Studio / PerfView ──> Inspect Retained Memory & GC Roots!

3Code Example

C# 13 & .NET 9
using System;

public class MemoryDiagnosticsDemo
{
    public static void Main()
    {
        long allocatedBytes = GC.GetTotalAllocatedBytes(precise: false);
        long currentMemory = GC.GetTotalMemory(forceFullCollection: false);

        Console.WriteLine($"Total Lifetime Allocated: {allocatedBytes / 1024} KB");
        Console.WriteLine($"Current Heap Footprint:   {currentMemory / 1024} KB");
    }
}

4Expected Output

Total Lifetime Allocated: 1240 KB
Current Heap Footprint:   480 KB

5Key Takeaways

  • `dotnet-gcdump` has near-zero overhead and can run in production.
  • Inspect object retention paths to find leaked event subscribers or static cache collections.
  • `GC.GetTotalAllocatedBytes()` enables precise benchmark tracking.