The ReferencePool class provides a mechanism for managing object pooling to reduce memory allocation and improve performance. It allows acquiring, releasing, adding, and removing references of specific types.
Install the latest version of the [CodeJunkie.ReferencePool] package from nuget:
dotnet add package CodeJunkie.ReferencePool- Object Pooling: Efficiently reuse objects to minimize memory allocation overhead.
- Type-Specific Pools: Manage pools for different types of objects.
- Thread-Safe Operations: Ensures safe access to the pool in multi-threaded environments.
- Statistics Tracking: Provides detailed information about the pool's usage, including counts for acquired, released, added, and removed references.
A static class that serves as the entry point for managing reference pools.
- Acquire: Retrieve an object from the pool.
- Release: Return an object to the pool after use.
- Add: Preload objects into the pool.
- Remove: Remove objects from the pool.
- ClearAll: Clear all reference pools.
An internal class that manages a specific type of reference.
- Tracks usage statistics such as:
- Unused references
- References in use
- Total acquired, released, added, and removed references
A struct that provides detailed information about a specific reference pool.
- Includes properties for:
- Reference type
- Counts for unused, in-use, acquired, released, added, and removed references
// Acquire a reference of type MyReference
var reference = ReferencePool.Acquire<MyReference>();
// Use the reference
reference.DoSomething();
// Release the reference back to the pool
ReferencePool.Release(reference);The ReferencePool class provides a mechanism for managing object pooling to reduce memory allocation and improve performance. It allows acquiring, releasing, adding, and removing references of specific types.
- Efficiently manage object pooling to minimize memory allocation overhead.
- Support for acquiring and releasing references.
- Ability to add and remove references dynamically.
- Strict type checking for reference management.
- Detailed statistics for reference pool usage.
CodeJunkie.ReferencePool
ReferencePool: The main static class for managing reference pools.IReference: Interface that all pooled objects must implement.ReferencePoolInformation: Struct that provides detailed information about the state of a reference pool.
All objects that will be managed by the ReferencePool must implement the IReference interface. This interface requires a Clear method to reset the object's state.
public class MyReference : IReference {
public int Value { get; set; }
public void Clear() {
Value = 0;
}
}