Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Garbage collection in .NET can be very expensive, especially for objects on the Large Object Heap (LOH). .NET itself already uses pooling for threads to prevent the costly creation of threads but instead re-uses the already created threads using a thread pool. Catel provides an implementation of pooling using the PoolManager<TPoolable>. This allows both Catel and third party developers to create a pool for large objects so they can be reused.

The documentation uses a byte array of 4096 as poolable object as example


Creating a pool manager

Catel implements pooling via the PoolManager<TPoolable> class. This class allows the caller to retrieve an object.

Creating a poolable object

Since the objects need to be re-used, it's very important that the PoolManager<TPoolable> knows how to reset objects to the initial state. Therefore every poolable object needs to implement IPoolable which also implements IDisposable. Below is an example implementation of a poolable object.

public class Buffer4096Poolable : IPoolable
{
    private const int BufferSize = 4096;


    protected IPoolManager _poolManager;


    public Buffer4096Poolable()
    {
        Data = new byte[BufferSize];
    }


    public byte[] Data { get; private set; }


    public int Size { get { return BufferSize; } }


    public void Reset()
    {
        var buffer = Data;
        Array.Clear(buffer, 0, buffer.Length);        
    }


    // Implemented explicitly so it can't be called accidentally
    void IPoolable.SetPoolManager(IPoolManager poolManager)
    {
        _poolManager = poolManager;
    }


    public void Dispose()
    {
        _poolManager.ReturnObject(this);
    }
}




  • No labels