ResourcePool Class |
Namespace: DigitalRune
The ResourcePool type exposes the following members.
Name | Description | |
---|---|---|
ResourcePool | Initializes a new instance of the ResourcePool class |
Name | Description | |
---|---|---|
Clear |
Removes all items from the resource pool.
| |
ClearAll |
Clears all resource pools.
| |
Equals | (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Name | Description | |
---|---|---|
Enabled |
Gets or sets a value indicating whether resource pooling is enabled.
| |
Pools |
Gets collection of all active ResourcePools.
|
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.
// 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.
// 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;