skip to content

As a lead on a C# codebase, when would you allow ArrayPool<T> pooling, and how would your team catch pool misuse before it reaches production?

level: principalimportance: should knowfreq 18%

answer

  1. GC savings versus lifetime bugs
  2. measure allocations first
  3. one owner per rental
  4. a strict pool in tests

basics

~20 s

Allow pooling only on hot paths where measured buffer allocation matters, keep each rental under one owner (try/finally or an IMemoryOwner<T>), and catch misuse with review rules, an injected checking pool in tests and the pool's event source in production.

solid answer

~50 s

Pooling trades garbage collector work for bugs the runtime will not catch: arrays that are longer than requested and dirty, double returns, and views that outlive the rental. I would allow `ArrayPool<T>` where an allocation profile shows buffers matter, such as stream loops, serializers, framing or per-frame game data, and prefer `stackalloc` or plain arrays for small synchronous scratch space. The code rule is one owner per rental: `Rent` and `Return` in one method with `Return` in a `finally` after the last awaited use, slices bounded by a count, and an `IMemoryOwner<T>` or disposable wrapper whenever the buffer crosses a component boundary. Because the shared pool checks only null and length, tests inject an `ArrayPool<byte>` subclass that tracks loans, throws on a second `Return` and poisons returned arrays. In production, `System.Buffers.ArrayPoolEventSource` shows pool sizing, not misuse.

code

csharp · 30 lines
csharp
using System.Buffers;

var pool = new CheckingPool();
byte[] chunk = pool.Rent(64 * 1024);
pool.Return(chunk);
try { pool.Return(chunk); }
catch (InvalidOperationException e) { Console.WriteLine(e.Message); }
Console.WriteLine($"outstanding: {pool.Outstanding}");

sealed class CheckingPool : ArrayPool<byte>
{
    private readonly HashSet<byte[]> _rented = new(ReferenceEqualityComparer.Instance);
    public int Outstanding { get { lock (_rented) return _rented.Count; } }

    public override byte[] Rent(int minimumLength)
    {
        byte[] array = Shared.Rent(minimumLength);
        lock (_rented) _rented.Add(array);
        return array;
    }

    public override void Return(byte[] array, bool clearArray = false)
    {
        lock (_rented)
            if (!_rented.Remove(array))
                throw new InvalidOperationException("Array returned twice or never rented here.");
        array.AsSpan().Fill(0xCD); // poison: a stale reader sees 0xCD, not plausible data
        Shared.Return(array);
    }
}

go deeper

for a junior

Know that pooling saves allocations but makes your code responsible for returning every array exactly once.

for a middle

Explain the costs a rental adds: dirty, oversized arrays and a return that must happen once, after the last use.

for a senior

Show how you would find a pooling bug: a checking pool in tests, concurrency stress with output hashes, and ownership traced in review.

for a principal

Own the trade-off: where pooling is allowed, what evidence justifies it, which code shapes are mandatory, and when to remove pooling that no longer pays.

## What pooling buys and what it costs Renting from `ArrayPool<T>` replaces "allocate a new array, let the garbage collector reclaim it" with "borrow an array and give it back". That trade is real in both directions. | | Plain `new byte[n]` | `ArrayPool<byte>.Shared` rental | |---|---|---| | Allocation per use | yes | usually none once the pool is warm | | Garbage collector work | proportional to allocation rate | lower | | Array contents | zeroed for you | whatever the last renter left | | Length | exactly `n` | at least `n` | | Lifetime bugs possible | none | double return, use after return, stale views | | Who frees it | the runtime | your code, exactly once | The first two rows are why teams pool; the last four are why a lead needs a rule. Large arrays add one more argument for pooling: arrays of 85,000 bytes or more go to the large object heap, which is only collected with the oldest generation (the details belong to the garbage collector material). ## When to allow it A workable rule is: **pool where measurement shows buffer allocation matters, and nowhere else.** - **Allow** it on hot paths that allocate buffers per request, per message or per frame: stream copy loops, serializers, compression, network framing, a game's per-frame scratch data. - **Allow** it for large, short-lived buffers whose allocation rate shows up in an allocation profile. - **Prefer something simpler** for small, fixed-size synchronous scratch space: a `stackalloc` span or a plain array. - **Refuse** it where the buffer's lifetime is unclear: callbacks, caches, fire-and-forget work, anything that stores the array. - **Require evidence**: an allocation measurement before and after (the benchmarking tools are covered with .NET performance work), not a belief that pooling is always faster. ## Shape the code so misuse is hard 1. **Rent and return in one method**, with `Return` in a `finally` that runs after the last awaited use. 2. **Never store a raw rented array** in a field, a closure, a queue or a cache. 3. **Bound every use by a count**: `buffer.AsSpan(0, count)`, never `buffer.Length`. 4. **Cross component boundaries only with an owner**: an `IMemoryOwner<T>` from `MemoryPool<T>.Shared`, or a small disposable type that wraps the rental, so the transfer is visible in a signature. 5. **Clear on return when the data is sensitive**: `Return(buffer, clearArray: true)` for tokens, keys and personal data. 6. **Isolate when sizes differ**: `ArrayPool<T>.Create(maxArrayLength, maxArraysPerBucket)` gives a subsystem its own limits, at the cost of not sharing arrays with the rest of the process. ## Catching misuse before production The pool will not help: `ArrayPool<T>.Shared` checks only null and the array length, and keeps no record of loans. So the team builds the checks itself. - **Inject the pool.** `ArrayPool<T>` is an abstract class with public `Rent` and `Return`, so components can take an `ArrayPool<byte>` (defaulting to `ArrayPool<byte>.Shared`) and tests can pass a checking subclass. - **Make the checking pool strict.** Track outstanding arrays by reference, throw on a second `Return` or on an array it never lent, fill returned arrays with a poison byte, and assert that nothing is outstanding when a test ends. - **Stress the concurrency.** Run many uploads in parallel against the checking pool and compare every output with a known hash; sharing bugs only show up under contention. - **Review against a checklist**: who owns this rental, where is its single `Return`, which awaits precede it, does any view escape. - **Watch the pool in production.** The `System.Buffers.ArrayPoolEventSource` events (`BufferRented`, `BufferAllocated` with a reason such as pool exhausted or over maximum size, `BufferReturned`, `BufferDropped`) show whether the pool is sized well. They cannot show a double return or a stale view. ## Writing the rule down A rule that lives only in a lead's head does not survive the next hire. A short entry in the team's conventions works better than a long essay, for example: - Pooled buffers are allowed in the listed hot-path components; anywhere else, a pull request that adds `ArrayPool<T>` links the allocation measurement that justifies it. - Every `Rent` has exactly one `Return` in a `finally` in the same method, or the rental is wrapped in an owner type the moment it is created. - Components that rent take an optional `ArrayPool<T>` so tests can pass the checking pool. ## When to take pooling out Pooling is a cost the team keeps paying in review time. If a path no longer shows up in allocation profiles, or keeps producing lifetime bugs, replacing the rental with a plain array is a legitimate simplification, not a regression.

  • Should library code use ArrayPool<T>.Shared directly or take a pool as a parameter?
    Default to `ArrayPool<T>.Shared`, because sharing one pool across components gives the best reuse, but expose an optional `ArrayPool<T>` so tests can substitute a checking pool. A private pool from `ArrayPool<T>.Create` isolates sizes and limits for one subsystem and gives up that sharing.
  • What can the ArrayPoolEventSource events tell you, and what not?
    They report rentals, allocations with a reason (the pool was exhausted, or the request exceeded its maximum size), returns, drops when a bucket is full, and trims. That answers sizing questions: are we allocating anyway, are arrays too big for the pool. They cannot reveal a double return or a stale view, because the pool never tracks loans.
  • Is renting worth it for a 256-byte scratch buffer in synchronous code?
    Usually not. A small, fixed-size synchronous buffer is simpler as a `stackalloc` span or a plain array, while a rental still brings the ownership rules, the dirty contents and the return. Pool it only if measurement shows that allocation matters.

  • Pool every array by default; allocation is always the bottleneck.
  • Pooling is safe once reviewers check that Return is called.
  • The GC can never reclaim a rented array that was not returned.
  • A private pool from ArrayPool<T>.Create() is always faster than Shared.
  • Event tracing will show us any double return in production.