Click or drag to resize
DigitalRuneResourcePool Class
Manages a pool of reusable items (base implementation).
Inheritance Hierarchy

Namespace: DigitalRune
Assembly: DigitalRune (in DigitalRune.dll) Version: 1.20.0.0 (1.20.1.14552)
Syntax
public abstract class ResourcePool

The ResourcePool type exposes the following members.

Constructors
  NameDescription
Protected methodResourcePool
Initializes a new instance of the ResourcePool class
Top
Methods
  NameDescription
Public methodClear
Removes all items from the resource pool.
Public methodStatic memberClearAll
Clears all resource pools.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Properties
  NameDescription
Public propertyStatic memberEnabled
Gets or sets a value indicating whether resource pooling is enabled.
Public propertyStatic memberPools
Gets collection of all active ResourcePools.
Top
Remarks

A resource pool can be used to remove load from the .NET garbage collector: Instead of allocating objects with new, objects are taken (see Obtain) from a pool of objects when the are needed and returned (see Recycle(T)) when they are no longer needed.

It is safe to obtain multiple objects without recycling them. In this case the objects are simply collected by the .NET garbage collector. However, the benefits of resource pooling are lost. To achieve optimal performance all objects, which are taken from the resource pool, should be returned to the resource pool.

Performance Considerations: Resource pooling should only be applied if there is strong evidence that the .NET garbage collector poses a performance bottleneck. On platforms, such as Windows, with a generational garbage collector it is in most cases counterproductive to use a resource pool. By keeping a large pool of objects on the heap, the time of single garbage collection is increased and the overhead created by the resource pooling is worse than allocating objects in .NET with new. But on some platforms, for example the .NET Compact Framework on the Xbox 360, a resource pool can help to avoid frequent full collections by the garbage collector.

Examples
The following demonstrates how to create a resource pool.
C#
// Resource pools are usually static members of a type.
public static readonly ResourcePool<List<float>> MyPool = 
  new ResourcePool<List<float>> (
    () => new List<float>(),   // Create
    null,                      // Initialize (not needed for List<float>)
    list => list.Clear());     // Uninitialize

Note: The example above creates a resource pools for ListT for a certain type. DigitalRune already provides such a resource pool (see ResourcePoolsT), so it is actually not required to create your own.

The following demonstrates how to use the resource pool created in example above.

C#
// Instead of calling 
//   List<float> myList = new List<float>(); 
// reuse existing list from resource pool.
List<float> myList = MyPool.Obtain();

// Do something with myList.
...

// Return myList to resource pool when no longer needed.
MyPool.Recycle(myList);
myList = null;
See Also