Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The pool manager internally uses a stack to manage the available objects in the pool. It's important to understand how a pool works. The flow diagram below shows how the pool manager deals with objects:

Gliffy
namePool Manager

Note

Note that the pool manager does not limit the number of objects in memory. It has a MaxSize property so it will store only a maximum amount of objects inside the internal pool, but if the pool is running out of instances and a new object is requested, it will return a new instance (and thus creating a new object which could be garbage collected).

Retrieving objects from the pool

Retrieving an object from the pool is very simple. When an instance of the PoolManager<TPoolable> is available, use the code below:

Code Block
var poolableBuffer = _poolManager.GetObject();

The PoolManager<TPoolable> will automatically create a new object when no objects are available in the pool.

Returning objects to the pool

...

Code Block
using (var poolableBuffer = _poolManager.GetObject())
{
    var buffer = poolableBuffer.Data;


    // work with the buffer here
}


// outside the scope, the object is automatically disposed and returned to the pool

The flow chart below shows how the PoolManager<TPoolable> will handle the dispose:

Gliffy
namePool Manager - returning objects

Customizing a pool manager

...