Advertisement
Advanced C#, Memory & Garbage Collection

How do Span<T> and ArrayPool reduce GC pressure?

Short answer

Span gives allocation-free views over existing memory (arrays, stack, native); ArrayPool rents and returns buffers instead of allocating. Both cut Gen 0 churn on hot paths.

Save C# mock interview
Advertisement
AModel answer
Span<T> gives allocation-free views over existing memory (arrays, stack, native); ArrayPool<T> rents and returns buffers instead of allocating. Both cut Gen 0 churn on hot paths.
var buffer = ArrayPool<byte>.Shared.Rent(4096);
try { Read(buffer); } finally { ArrayPool<byte>.Shared.Return(buffer); }
Real-world example
A high-throughput parser reuses pooled buffers to avoid allocating per request.