In ASP.NET Core 10, when does Microsoft.Extensions.ObjectPool's ObjectPool<T> help a hot endpoint, and how do its retention limit and IResettable behave?
answer
- expensive to create, used constantly
- Get() never blocks, creates when empty
- MaximumRetained = ProcessorCount * 2
- IResettable.TryReset() on Return
- objects of 85,000 bytes or more hit the LOH
basics
~20 sObjectPool<T> pays off only for objects that are expensive to create and used on every request, such as large StringBuilders. Get creates a new object when the pool is empty; Return keeps at most ProcessorCount * 2 and calls IResettable.TryReset first.
solid answer
~50 s`Microsoft.Extensions.ObjectPool` ships in the ASP.NET Core shared framework. `ObjectPool<T>.Get()` hands out a retained object or, if none is left, **creates a new one**; it never blocks. `Return(obj)` asks the policy whether the object may be reused and then keeps it only if the pool is below its retention limit; `DefaultObjectPoolProvider.MaximumRetained` defaults to `Environment.ProcessorCount * 2`. So the pool limits what it **retains**, not what it allocates. If `T` implements **`IResettable`**, the default policy calls `TryReset()` on return and drops the object when it returns `false`; otherwise you write a `PooledObjectPolicy<T>` with `Create` and `Return`. Pooling pays off only for objects that are expensive to create and used on hot paths, such as the `StringBuilder` pool from `CreateStringBuilderPool`; for cheap objects, `Get`/`Return` is slower than `new`. Measure first, and never use an object after returning it.
code
csharp · 17 linespublic sealed class ReportSummary(ObjectPool<StringBuilder> pool)
{
public string Build(IReadOnlyList<Shipment> shipments)
{
var sb = pool.Get();
try
{
foreach (var s in shipments)
sb.Append(s.Id).Append(';').Append(s.Status).AppendLine();
return sb.ToString();
}
finally
{
pool.Return(sb); // policy clears it; oversized builders are dropped
}
}
}go deeper
Know that object pooling reuses expensive objects and that you must return them, usually in a finally block.
Explain Get and Return semantics, the default retention limit, IResettable versus a custom policy, and why cheap objects should not be pooled.
Decide from measurements whether pooling or streaming fixes an allocation problem, and guard against use-after-return and state leaks.
Set guidance on when teams may introduce pooling, given its correctness risks, versus simpler fixes like streaming and pagination.
## Why pool at all Every allocation on a hot path costs CPU to allocate and, later, garbage-collection work to reclaim. For most objects that cost is tiny and the GC handles it well. A few objects are different: they are **expensive to create or initialise**, carry large internal buffers, or are created on every request. Reusing them can reduce allocation rate and GC pauses. That is what **`Microsoft.Extensions.ObjectPool`** is for. It is part of the ASP.NET Core shared framework, and ASP.NET Core uses it internally, for example to reuse `StringBuilder` instances. The docs are explicit that pooling does not always help: unless initialisation is expensive, getting an object from the pool is usually slower than allocating it, and pooled objects stay in memory until the pool itself goes away. Use it only after measuring. ## How the pool behaves | Member | Behaviour in .NET 10 | |---|---| | `ObjectPool<T>.Get()` | Returns a retained object, or creates one through the policy when none is retained. Never blocks, never fails for lack of objects | | `ObjectPool<T>.Return(T)` | Asks the policy's `Return`; if that says no, or the pool already holds its maximum, the object is dropped | | `DefaultObjectPoolProvider.MaximumRetained` | `Environment.ProcessorCount * 2` by default | | `IResettable.TryReset()` | Called by `DefaultPooledObjectPolicy<T>` on return; `false` means the object is discarded | | `ObjectPool.Create<T>()` | Creates a pool with the default provider and policy | Two consequences follow: - The pool limits **retained** objects, not **allocated** ones. Under a burst, `Get` keeps creating new objects; on return, the extras are dropped for the GC. - If `T` is `IDisposable`, the default provider creates a pool that **disposes** objects it does not retain, and disposes retained ones when the pool is disposed. ## Wiring it into an app A common pattern registers the provider and a pool as singletons, then injects `ObjectPool<T>` where it is needed: ```csharp builder.Services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>(); builder.Services.TryAddSingleton(sp => sp.GetRequiredService<ObjectPoolProvider>().CreateStringBuilderPool()); ``` `CreateStringBuilderPool()` uses `StringBuilderPooledObjectPolicy`, whose defaults are an `InitialCapacity` of 100 characters and a `MaximumRetainedCapacity` of 4 * 1024: a builder that grew beyond that is not kept, so one huge report does not pin a huge buffer forever. ## The rules that keep pooling safe 1. **Always return in a `finally`.** An object that is never returned is simply garbage collected, which is safe but wastes the pool. 2. **Never touch an object after `Return`.** Another request may already be using it; this is a data-corruption bug, not a performance one. 3. **Reset completely.** Leftover state from one request leaking into the next is the classic pooling defect; `IResettable.TryReset` or the policy's `Return` must clear everything. 4. **Do not pool cheap objects.** Small DTOs and short-lived lists are cheaper to allocate than to pool. ## Measuring before and after Pooling is justified by numbers, not by intuition: 1. measure the allocation rate and garbage-collection counts of the endpoint under a load test; 2. confirm that the candidate object is a real share of those allocations; 3. introduce the pool and rerun the same test, checking both throughput and latency percentiles; 4. remove the pool again if the gain is within noise, because it adds correctness risk for nothing. ## Large buffers on hot paths The related problem is **large buffers**. In .NET, an allocation of **85,000 bytes or more** goes to the large object heap, which is reclaimed only by full collections. Reading a whole request or response body into one `byte[]` or `string` per request does this on every call. The fixes in an ASP.NET Core app are to stream bodies instead of buffering them, to keep responses paginated, and, where a big temporary buffer is truly needed, to rent it from a pool rather than allocate it each time. In the freight dashboard, the CSV export built each report in a fresh `StringBuilder` that grew to several hundred kilobytes. Streaming rows straight to the response removed most of that allocation; pooling then helped only for the small summary builders used on every request.
- What happens when 500 concurrent requests call Get() on a pool that retains 16 objects?Nothing blocks. `Get` hands out the retained objects and then creates new ones through the policy for the rest. On `Return`, the pool keeps objects only until it again holds its maximum; the extras are dropped and collected, or disposed if `T` is `IDisposable` and the default provider built the pool.
- When is IResettable better than a custom PooledObjectPolicy<T>?When you own the pooled type and the reset logic belongs with it: implement `TryReset()` to clear state and return `false` when the object should not be reused. A custom `PooledObjectPolicy<T>` fits types you cannot change, or when creation needs parameters, because it supplies both `Create()` and `Return()`.
- ObjectPool<T>.Get blocks until another request returns an object.
- MaximumRetained caps the total number of objects the pool will ever create.
- Pooling every DTO on a hot path always reduces latency.
- Using a pooled object after Return is safe as long as you only read from it.
- ObjectPool<T> requires installing a separate package outside the ASP.NET Core shared framework.