Simple and easy to understand object pool implementation.
You might need and object pool when you want to reduce garbage collections by reusing objects rather than creating and destroying them. The goal of this project is to make an easy to use and understand object pool.
This implementation is thread safe, supports async and partially lockless.
-
Create an empty pool with a factory, or a pool with a fixed collection of objects, or a warmed up pool.
-
Take the object and use it.
-
Dispose the pool item to release the object.
var pool = new ObjectPool<MyObject>(() => new MyObject());
using (pool.Take(out var item))
{
// Use the item
}
If your pooling type derives from IClearable, it will be cleared before returning to the pool.
public class MyObject : IClearable
{
public void Clear()
{
// Do your clearing here
}
}