ArrayPool in C#: renting buffers without corrupting them
ArrayPool in C# removed a full GC every 24 calls at 128 KiB in one benchmark run. The rent and return pattern that keeps a shared buffer from corrupting data.
Two SHA-256 digests of the word hello disagree: 2CF24DBA5FB0A30E... is correct, and
E2DD78C2D4548BB6... came out of a pooled buffer. The code asked ArrayPool<byte>.Shared for 5 bytes,
received an array of 16, and hashed all of it. Picture that
line arriving in a refactor that replaced new byte[length] with a pool call to reduce garbage. The
unit tests that checked lengths still pass, and now every signature is computed over bytes nobody
meant to sign.
ArrayPool<T> is a loan desk for arrays. Renting an array you hand back later can remove an
allocation from a hot path, which is a real gain once you know that allocation matters. The price is
that a plain array, which the type system treats as yours forever, now has a lifetime only your code
enforces. Every bug in this post comes from forgetting one of the terms of that loan.
static byte[] Hash(ReadOnlySpan<byte> payload)
{
var buffer = ArrayPool<byte>.Shared.Rent(payload.Length);
try
{
payload.CopyTo(buffer);
return SHA256.HashData(buffer.AsSpan(0, payload.Length));
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}The requested length is kept and used to slice, so nothing reads past the data. The return sits in
finally, so an exception cannot lose the array. And the buffer
never leaves the method, which makes the ownership question trivial to answer.
A rented buffer is a lease with one owner
-
Rent(n)
You own an array of at least n elements, with old contents.
-
Use the first n
Slice to n. Never trust buffer.Length.
-
Return once
From a finally block. Ownership ends here.
Ways to break the lease
Use after return
A stale reference writes into an array the next renter now owns.
Double return
The pool holds one array twice and hands it to two renters.
Never returned
No crash. The GC collects it and the pool allocates a replacement.
The documentation for Rent
promises an array of at least the requested length, and the
shared pool
says it may hand back a larger one. Here is what the shared byte pool returned on .NET 10:
Rent(1) -> 16
Rent(17) -> 32
Rent(100) -> 128
Rent(1000) -> 1024
Rent(5000) -> 8192
Rent(1048576) -> 1048576
Rent(1048577) -> 2097152Powers of two is what this version does, and it is an implementation detail you should not code
against. What you can rely on is the rule: buffer.Length describes the array, and your data has its
own length. Any call that takes the whole array is a suspect in review, including SHA256.HashData(buffer),
stream.Write(buffer), Encoding.UTF8.GetString(buffer) and new MemoryStream(buffer). Pass a span
or an explicit count.
The rounding also has a memory cost. One byte past 1 MiB rents 2 MiB, so a pool sized around values just over a power of two holds nearly double the memory you asked for.
Rent may return an array that is not zero-initialized,
and in practice it usually holds whatever the last renter left. This ran in a single process, on one
thread:
var first = ArrayPool<byte>.Shared.Rent(4096);
Encoding.UTF8.GetBytes("Bearer eyJhbGciOi-session-token", first);
ArrayPool<byte>.Shared.Return(first);
var second = ArrayPool<byte>.Shared.Rent(4096);
Console.WriteLine(ReferenceEquals(first, second)); // True
Console.WriteLine(Encoding.UTF8.GetString(second, 0, 31)); // Bearer eyJhbGciOi-session-tokenThat has two consequences. Code must never assume a rented array is zeroed, so a parser that treats a zero byte as the end of its data can read stale input from another request. And anything sensitive left in a pooled array is one over-read away from a response body.
Return(buffer, clearArray: true) looks like the fix for secrets, but read the
remarks on Return:
it clears only if the pool will store the buffer. If the pool releases it, the contents stay as they
were. A custom pool limited to 4,096 bytes shows it: an oversize array returned with clearArray: true
kept its data, while a pooled one came back zeroed. For keys and tokens, wipe the
range yourself before returning, using
CryptographicOperations.ZeroMemory:
var key = ArrayPool<byte>.Shared.Rent(keyLength);
try
{
RandomNumberGenerator.Fill(key.AsSpan(0, keyLength));
// use the key
}
finally
{
CryptographicOperations.ZeroMemory(key.AsSpan(0, keyLength));
ArrayPool<byte>.Shared.Return(key);
}Clearing matters for reference types as well. In a test with ArrayPool<object>, an object stored in a
returned, uncleared array survived a forced full collection, because the pool still held the array
that pointed at it. The same test with clearArray: true let the object be collected.
The documentation treats both as high-severity security issues and links them to the double-free and use-after-free weaknesses from native code. Managed memory rules out wild pointers, but the data corruption is the same, and the pool will not save you:
var buffer = ArrayPool<byte>.Shared.Rent(8192);
ArrayPool<byte>.Shared.Return(buffer);
ArrayPool<byte>.Shared.Return(buffer); // no exception
var a = ArrayPool<byte>.Shared.Rent(8192);
var b = ArrayPool<byte>.Shared.Rent(8192);
Console.WriteLine(ReferenceEquals(a, b)); // TrueTwo callers now write into one array, and each sees the other's bytes. Use after return behaves the same way: a stale reference read 42 from the slot where the next renter had just written 42. Neither mistake fails at the line that caused it. The symptom turns up later in an unrelated request, often only under load, and a single-threaded unit test can pass for years.
The pool does check one thing. Returning new byte[1000] threw ArgumentException, because no bucket
has that size. Returning new byte[1024] was accepted without complaint, so the check cannot tell you
an array is really a pool's own.
Async code is where these bugs tend to appear, because a method can return while something else still holds the buffer. Microsoft's Memory and Span guidelines frame this as ownership and leases: one owner, and consumers that may touch the buffer only during their lease. When a buffer must outlive one method, make that owner a type:
public sealed class PooledBuffer : IDisposable
{
private byte[]? _array;
public PooledBuffer(int length)
{
_array = ArrayPool<byte>.Shared.Rent(length);
Length = length;
}
public int Length { get; }
public Span<byte> Span =>
(_array ?? throw new ObjectDisposedException(nameof(PooledBuffer))).AsSpan(0, Length);
public void Dispose()
{
var array = Interlocked.Exchange(ref _array, null);
if (array is not null)
{
ArrayPool<byte>.Shared.Return(array);
}
}
}A second Dispose becomes a no-op, access after disposal throws, and the slice always has the
requested length. A span taken before disposal can still outlive the owner, so this narrows the risk
without removing it. MemoryPool<T> with IMemoryOwner<T> is the framework's version of the same idea.
Each method gets a scratch buffer, copies a source array into it and sums the bytes. Measured with BenchmarkDotNet 0.15.8 (Release, .NET 10.0.9, Windows 11, Ryzen 9 9950X3D), in a single pass while other jobs shared the machine:
| Size | Method | Mean | Allocated | Gen 2 per 1,000 calls |
|---|---|---|---|---|
| 1 KiB | new byte[] |
327.9 ns | 1,048 B | 0 |
| 1 KiB | Shared pool | 275.8 ns | 0 B | 0 |
| 16 KiB | new byte[] |
4,910 ns | 16,408 B | 0 |
| 16 KiB | Shared pool | 4,165 ns | 0 B | 0 |
| 128 KiB | new byte[] |
79,712 ns | 131,110 B | 41.6 |
| 128 KiB | Shared pool | 34,525 ns | 8 B | 0 |
| 1 MiB | new byte[] |
358,094 ns | 1,048,677 B | 248.5 |
| 1 MiB | Shared pool | 269,876 ns | 0 B | 0 |
The time column is the weaker evidence, given how the run was done. Copying and summing take much of the time, and this run does not explain why the gap is widest at 128 KiB, where the fresh array takes more than twice as long, and narrower at 1 MiB. The garbage column is clearer. At 128 KiB the array crosses the 85,000-byte large object threshold, and 41.6 gen 2 collections per 1,000 calls works out to one full collection every 24 calls. At 1 MiB it is one every 4. The post on .NET garbage collection explains why that pattern hurts tail latency, and a real server with a large heap would pay more per collection than this tiny benchmark did.
At 128 KiB a fresh array caused one gen 2 collection every 24 calls in this run. The pooled version caused none.
Passing clearArray: true added about 12.5 microseconds at 1 MiB and nothing measurable at 16 KiB. This setup also flatters the pool. Every call rents and returns on one
thread with no contention, so the buffer is nearly always sitting ready. Renting on one thread and
returning on another was not measured here and could look different.
For a small, fixed-size scratch buffer, stackalloc with a span
avoids both the heap and the pool. For code that runs at startup or a few times a minute, plain
new byte[n] is simpler and has none of the failure modes above. A pool also keeps arrays alive by
design, so a burst of large rentals can leave your memory use higher for a while after the burst ends.
Some rentals disappear once you question the copy, since an aggregate over an array can often run in place. The kata linked from this post streams 200,000 numbers under a 64 KB allocation budget, which a sorted clone blows through. Writing fast C# explains how those budgets are reported, and the C# coding challenges list others with the same gate.
Practice what you just read
- Open in the editor: Trimmed Sum
Algorithm Easy Free, no account needed
Trimmed Sum
Sum everything except one min and one max - without sorting a copy.
More like this: C# coding challenges →