...
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 | ||||
---|---|---|---|---|
|
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 | ||||
---|---|---|---|---|
|
Customizing a pool manager
...