The intelligent pooling system provides automatic memory management for WallstopGenericPool<T> instances. Instead of pools growing unbounded or requiring manual purge calls, the system:
Tracks usage patterns - Monitors high-water marks and access frequency
Purges intelligently - Only removes items unlikely to be needed soon
Spreads work - Limits purges per operation to avoid GC spikes
Responds to pressure - Aggressive cleanup when memory is low
Respects object size - Large objects get stricter policies
usingWallstopStudios.UnityHelpers.Utils;// Pools automatically use intelligent purgingvarpool=newWallstopGenericPool<List<int>>(createFunc:()=>newList<int>(),actionOnGet:list=>list.Clear());// Rent and return as usual - purging happens automaticallyvarlist=pool.Get();list.Add(1);list.Add(2);pool.Release(list);
usingWallstopStudios.UnityHelpers.Utils;// Configure specific type behaviorPoolPurgeSettings.Configure<ExpensiveObject>(options=>{options.IdleTimeoutSeconds=600f;// 10 minutesoptions.MinRetainCount=5;// Always keep 5options.WarmRetainCount=10;// Keep 10 when active});// Configure all List<T> variantsPoolPurgeSettings.ConfigureGeneric(typeof(List<>),options=>{options.IdleTimeoutSeconds=120f;// 2 minutesoptions.BufferMultiplier=1.5f;// 50% buffer});// Disable purging for specific typesPoolPurgeSettings.Disable<CriticalResource>();
Items that have been idle longer than IdleTimeoutSeconds are purged regardless of comfortable size. The comfortable size primarily influences the target retention during non-idle purges and memory pressure events.
After a usage spike, purging is suppressed for HysteresisSeconds to prevent purge-allocate cycles:
sequenceDiagram
participant App as Application
participant Pool as Pool
Note over App,Pool: Normal usage period
App->>Pool: Get items (low volume)
Pool->>Pool: Track high-water mark
Note over App,Pool: Usage spike detected
App->>Pool: Get many items rapidly
Pool->>Pool: Spike! Start hysteresis
Note over App,Pool: Hysteresis period (2 min default)
Pool->>Pool: Purging suppressed
Note over App,Pool: After hysteresis
Pool->>Pool: Resume normal purging
// Configure max items purged per operationoptions.MaxPurgesPerOperation=10;// Pool tracks pending purgesif(pool.HasPendingPurges){// More items to purge on next trigger}// Force immediate full purge (bypasses limit)pool.ForceFullPurge();
usingWallstopStudios.UnityHelpers.Utils;// Estimate single item sizelongsize=PoolSizeEstimator.EstimateItemSizeBytes<MyLargeObject>();// Estimate array sizelongarraySize=PoolSizeEstimator.EstimateArraySizeBytes<byte>(length:100000);// Check if on LOHboolisLargeObject=size>=PoolPurgeSettings.LargeObjectThresholdBytes;
usingWallstopStudios.UnityHelpers.Utils;// Configure global budgetPoolPurgeSettings.GlobalMaxPooledItems=50000;// Get global statisticsGlobalPoolStatisticsglobalStats=GlobalPoolRegistry.GetStatistics();inttotalPooled=globalStats.TotalPooledItems;floatbudgetUtilization=globalStats.BudgetUtilization;intregisteredPools=globalStats.RegisteredPoolCount;// Force budget enforcementGlobalPoolRegistry.EnforceBudget();// Try non-blocking budget checkif(GlobalPoolRegistry.TryEnforceBudgetIfNeeded()){// Budget was over, items purged}