Skip to main content

Object pooling for .NET

Good objects.
Back in circulation.

Create it once. Use it again. Reservoir keeps your objects ready for the next piece of work, with thread-safe pooling and bounded shared retention. Scoped rentals and opt-in manual rentals also retain objects in per-pool thread-local storage.

dotnet add package Reservoir

.NET Standard 2.0-compatible runtimes, with dedicated .NET 8 and .NET 10 assets. Package compatibility.

An object, back in circulationEight reusable objects are available in the pool. Rent one to follow its journey.RentReturnYour codeShared pool8 retention slots12345678
8 available / 0 in useSame objects. Ready for more work.

Try the lifecycle. This illustration starts with a warm pool.

Compare workloads, timings, and allocations.

See the benchmarks and methodology

A short stay in your code.

Rent an object, do your work, and give it back. For synchronous work, a scoped lease handles the return when you leave scope.

  1. RentReuse an available object, or create one on a miss.
  2. WorkThe object belongs to you until you return it.
  3. ReturnReset it for reuse. Discard it if it no longer fits.
Understand the ownership rules
A list, on loanC#
using Reservoir;

using var lease = ListPool<int>.Shared
.RentScoped(out List<int> numbers);

numbers.Add(42);
Consume(numbers);

// Leaving scope returns the list.
// The next renter gets an empty one.

Crossing an await? Use Rent / Return with try / finally.

Your everyday objects.
Already covered.

Built-in pools for collections and text. Rent them empty, return them for reuse, and set limits on what stays cached.

Browse collection and text pools
List<T>Dictionary<TKey, TValue>HashSet<T>Queue<T>Stack<T>StringBuilder

Also included: CancellationTokenSource pooling.

Something of your own? Define a creation and reset policy.

Make yourself at home.