C# › Memory & Garbage Collection

How would you use a memory profiler (or dotnet-counters / dotnet-gcdump) to diagnose excessive garbage collection pressure in a production .NET application?

Advanced

Answer

Tools like dotnet-counters can monitor real-time GC metrics (Gen 0/1/2 collection counts, allocation rate, time in GC) to identify if GC pressure is a genuine bottleneck; dotnet-gcdump captures a full heap snapshot for offline analysis, letting you identify which object types are consuming the most memory or being allocated at the highest rate, pointing directly at the code responsible.
# Monitor live GC metrics
dotnet-counters monitor -p <process-id> --counters System.Runtime

# Capture a heap dump for detailed offline analysis
dotnet-gcdump collect -p <process-id>
Real-world example Diagnosing a production API experiencing intermittent latency spikes, traced back to frequent Gen 2 collections caused by excessive large object allocations.

Common follow-ups: What specific GC-related metric would most directly indicate that allocation rate itself (not object retention) is the core problem?