If you’ve ever tuned a high-throughput .NET service, you already know the cost of allocation churn. Repeatedly allocating short-lived arrays (e.g., for file I/O, network reads, JSON payloads) drives GC pressure, latency spikes, and cache misses. ArrayPool<T> gives you a lightweight way to reuse arrays instead of recreating them, often a “free win” with minimal code changes.
Below I'll explain when to use ArrayPool<T>, the gotchas that bite, and gives you copy-pasteable examples you can drop into APIs, background workers, and stream pipelines.
What is ArrayPool<T>?
ArrayPool<T> (in System.Buffers) is a shared or custom pool that lets you:
Rent an array at least a requested size.
Return the array when you’re done (optionally clearing it).
You avoid allocating new arrays on every loop iteration. The pool hands you a previously used buffer, which dramatically reduces GC work under load.
using System.Buffers;
var pool = ArrayPool<byte>.Shared;
byte[] buffer = pool.Rent(64 * 1024); // request ~64KB
try
{
// use buffer[0..n]
}
finally
{
pool.Return(buffer, clearArray: false);
}
Tip: Always wrap the lifetime with
try/finallyto guarantee the buffer is returned, even on exceptions.
When should you use it?
Use ArrayPool<T> when you:
Allocate the same size arrays repeatedly (e.g., 8KB–1MB buffers).
Process streams (files, sockets, HTTP) in a loop.
Handle bursty workloads where GC pauses hurt tail latency.
Move large binary payloads (images, PDFs, message packs, Protobuf).
It’s overkill for tiny, one-off arrays or when code clarity matters more than micro-optimisations.
Core behaviours you must know
Size is a minimum, not exact
Rent(minLength)can return a larger array. Use only the slice you filled.Buffers are not zeroed
Data from a previous renter may be present. If you hold sensitive data (tokens, PII), either:
set
clearArray: trueonReturn, ormanually clear the used slice.
Return exactly once
Never double return or keep references after returning. Treat the array as invalid once returned.Thread safety
ArrayPool<T>.Sharedis thread-safe, but your buffer usage must be too. Don’t share a single rented buffer across concurrent operations unless you synchronise access.
Example 1. Efficient stream copy with a pooled buffer
Classic pattern for copying streams (file uploads, proxies, etc.):
using System.Buffers;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
public static class StreamCopy
{
private const int DefaultBufferSize = 64 * 1024; // 64KB
public static async Task CopyAsync(Stream source, Stream destination,
CancellationToken ct = default)
{
byte[] buffer = ArrayPool<byte>.Shared.Rent(DefaultBufferSize);
try
{
int read;
while ((read = await source.ReadAsync(buffer.AsMemory(0, buffer.Length), ct)) > 0)
{
await destination.WriteAsync(buffer.AsMemory(0, read), ct);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer, clearArray: false);
}
}
}
Why this helps: under load, you avoid allocating a new 64KB on every loop iteration. That’s a lot of GC pressure gone.
Example 2. Reading a file without ballooning memory
Read file contents, then trim to exact size only at the very end:
using System.Buffers;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
public static class FileReader
{
// Reads entire file; returns a tightly sized array.
public static async Task<byte[]> ReadAllBytesPooledAsync(
string path, int chunkSize = 128 * 1024, CancellationToken ct = default)
{
byte[] rented = ArrayPool<byte>.Shared.Rent(chunkSize);
try
{
await using var fs = File.OpenRead(path);
int total = 0;
int read;
// If file is larger than one chunk, we’ll grow via Array.Resize on a temporary
// *owned* array, but keep reusing a rented chunk buffer for reads.
byte[] owned = new byte[0];
while ((read = await fs.ReadAsync(rented.AsMemory(0, rented.Length), ct)) > 0)
{
int required = total + read;
if (owned.Length < required)
{
int newSize = owned.Length == 0 ? required : System.Math.Max(required, owned.Length * 2);
System.Array.Resize(ref owned, newSize);
}
rented.AsSpan(0, read).CopyTo(owned.AsSpan(total));
total += read;
}
// Trim to exact size
if (owned.Length != total)
System.Array.Resize(ref owned, total);
return owned;
}
finally
{
ArrayPool<byte>.Shared.Return(rented);
}
}
}

Dave BlackPosted Nov 5, 2026, 10:36 AM
Your Example 1 has a bug in it. you should not read past the length of the buffer you requested since the buffer provided may be larger than the buffer requested. Example 1 uses: while ((read = await source.ReadAsync(buffer.AsMemory(0, buffer.Length), ct)) > 0) when it should read: while ((read = await source.ReadAsync(buffer.AsMemory(0, DefaultBufferSize), ct)) > 0)