What is GC pressure and how do you diagnose and fix it?

Answer

GC pressure occurs when an application allocates objects faster than the GC can collect them, or allocates many short-lived objects that trigger frequent Gen 0 collections, impacting throughput and adding latency spikes. Diagnosis: (1) dotnet-counters: dotnet-counters monitor -p PID — watch gen-0-gc-count, gen-1-gc-count, gen-2-gc-count. (2) dotnet-trace: collect GC events. (3) Memory profiler: JetBrains dotMemory, Visual Studio Diagnostic Tools — identify allocation hot spots. (4) EventSource GC events. Common causes: (1) String concatenation in loops. (2) LINQ creating intermediate collections. (3) Boxing value types. (4) Closures capturing large objects. (5) Oversized arrays. (6) Large object heap fragmentation. Fixes: Span<T>, ArrayPool<T>, StringBuilder, struct instead of class, object pooling (ObjectPool<T>), RecyclableMemoryStream, avoid LINQ in hot paths, reduce closure captures.