Search code examples
c#.netobjectpool

Why doesn't Microsoft.Extensions.ObjectPool appear to do anything?


Using Microsoft.Extensions.ObjectPool v7.0.11 and running the following statements in LINQPad v7.7.15

var pool = ObjectPool.Create<object>();
var thing = pool.Get();
pool.Return(thing);
pool.Uncapsulate()._items.Dump();

The result is

ObjectWrapper<Object>[47] = { null, null, null, null, null, null, ... }

Shouldn't I be seeing the object in the internal list of items?


Solution

  • You're messing around with undocumented internals of the ObjectPool. Looks like there's an optimization for the case where a single object is checked in and out frequently, and it's assigned to a field _firstItem instead of being added to _items.

    enter image description here