Object Pool
Introduction
The object pool is a crucial component for optimizing memory management and performance. It improves application performance by reusing objects to reduce memory allocation and garbage collection.
Core Features
Object Pool Definition
csharp
public class ObjectPool: LogicSingleton<ObjectPool>, ISingletonAwake
{
private ConcurrentDictionary<Type, Pool> objPool;
private readonly Func<Type, Pool> AddPoolFunc = type => new Pool(type, 1000);
}
Main APIs
1. Fetch Objects
csharp
// Fetch via generic type
T obj = ObjectPool.Instance.Fetch<T>();
// Fetch via specific type
MyClass obj = ObjectPool.Instance.Fetch(typeof(MyClass));
// Create new object without using pool
MyClass obj = ObjectPool.Instance.Fetch(typeof(MyClass), false);
2. Recycle Objects
csharp
// Recycle object back to pool
ObjectPool.Instance.Recycle(obj);
Usage Examples
1. Basic Usage
csharp
// 1. Define poolable object
public class MyClass : IPool
{
public bool IsFromPool { get; set; }
// Other properties and methods
public string Name { get; set; }
public int Value { get; set; }
}
// 2. Fetch object
MyClass obj = ObjectPool.Instance.Fetch<MyClass>();
obj.Name = "Test Object";
obj.Value = 100;
// 3. Use object
// ... Code using the object ...
// 4. Recycle object
ObjectPool.Instance.Recycle(obj);
2. Practical Example
The codebase includes a ListComponent.cs
file that implements a poolable generic list component:
csharp
public class ListComponent<T>: List<T>, IDisposable
{
public ListComponent()
{
}
public static ListComponent<T> Create()
{
return ObjectPool.Instance.Fetch(typeof (ListComponent<T>)) as ListComponent<T>;
}
public void Dispose()
{
if (this.Capacity > 64) // Let GC handle if capacity exceeds 64
{
return;
}
this.Clear();
ObjectPool.Instance.Recycle(this);
}
}
This component demonstrates excellent usage of the object pool:
Smart Pool Management:
- Uses static
Create()
method to fetch instances from the pool - Implements
IDisposable
interface for automatic recycling withusing
statements - Intelligently decides whether to recycle based on capacity
- Uses static
Performance Optimization:
- Lists with capacity over 64 are handed to GC, preventing large objects from occupying pool space
- Calls
Clear()
before recycling, ensuring a clean state for next use
Usage Example:
csharp
using (var list = ListComponent<int>.Create())
{
list.Add(1);
list.Add(2);
list.Add(3);
// Automatically recycled back to pool after use
}
Implementation Principles
1. Lock-free Design
- Uses
ConcurrentDictionary
to store type-specific object pools - Uses
ConcurrentQueue
to manage pooled objects - Implements lock-free operations using
Interlocked
2. Fast Path Optimization
- Maintains
FastItem
for quick object acquisition and return - Uses
CompareExchange
to ensure thread safety
3. Capacity Management
- Each type-specific pool has a maximum capacity limit
- Objects exceeding capacity are discarded for garbage collection
Best Practices
Usage Guidelines
Object Design:
- Implement
IPool
interface to support pool management - Objects should be reusable, avoid maintaining state
- Reset object state before recycling
- Implement
Performance Optimization:
- Set appropriate pool capacity
- Avoid frequent creation and destruction of large objects
- Use
using
statement for automatic object lifecycle management
Concurrency Handling:
- Leverage thread-safe features of the object pool
- Handle object recycling correctly in exception cases
Important Notes
WARNING
- Ensure recycled objects are no longer referenced
- Avoid accessing objects after recycling
- Watch for memory leaks, ensure objects are properly recycled
- Pooled objects should be lightweight, avoid pooling large objects
Technical Support
Get Help
- 💬 Join QQ Group Discussion
(ET Framework Group)
: 474643097 - ⭐ Follow the project on GitHub for latest updates