High-throughput web services processing streams or JSON payloads often allocate thousands of temporary byte[] instances per second. Under high concurrency, these transient allocations hammer Gen0 and Gen1, triggering frequent garbage collection pauses.
Before: Constant Heap Allocations
byte[] buffer = new byte[4096];
int read = await stream.ReadAsync(buffer);
ProcessData(buffer.AsSpan(0, read));
After: Rented Pooled Buffers
byte[] buffer = ArrayPool<byte>.Shared.Rent(4096);
try
{
int read = await stream.ReadAsync(buffer);
ProcessData(buffer.AsSpan(0, read));
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
Why It Matters
-
Zero Allocations:
ArrayPool<T>.Sharedchecks out existing buckets in memory, eliminating per-request allocations entirely. - LOH Avoidance: Renting buffers greater than 85,000 bytes prevents large objects from fragmenting the Large Object Heap.
-
Actual Size vs Requested Size: Rented arrays round up to powers of two. Always pass the actual bytes read using
AsSpan(0, bytesRead)rather than readingbuffer.Length.
Watch out: Return(buffer) does not zero out contents by default. If your buffer handles tenant data or tokens, pass clearArray: true to avoid data leakage.
Do you use ArrayPool<T>.Shared or pipe streams directly via PipeReader?

Top comments (0)