nlsq.caching.memory_manager.MemoryManager¶
- class nlsq.caching.memory_manager.MemoryManager(gc_threshold=0.8, safety_factor=1.2, enable_adaptive_safety=False, disable_padding=False, memory_cache_ttl=1.0, adaptive_ttl=True)[source]¶
Bases:
objectIntelligent memory management for optimization algorithms.
This class provides: - Memory usage monitoring and prediction - Array pooling to reduce allocations with LRU eviction - Automatic garbage collection triggers - Context managers for memory-safe operations
LRU Memory Pool (Task Group 7 - 1.2a)¶
The memory pool uses an OrderedDict to track access order, enabling true LRU (Least Recently Used) eviction when at capacity. This improves cache utilization for frequently accessed array shapes by 5-10%.
Telemetry Circular Buffer (Task Group 9 - 1.3a)¶
The safety telemetry uses a deque with maxlen=1000 to prevent memory leak in multi-day optimization runs. This maintains the last 1000 telemetry records for adaptive safety factor calculation.
- memory_pool¶
Pool of reusable arrays indexed by (shape, dtype) with LRU tracking
- Type:
OrderedDict
- __init__(gc_threshold=0.8, safety_factor=1.2, enable_adaptive_safety=False, disable_padding=False, memory_cache_ttl=1.0, adaptive_ttl=True)[source]¶
Initialize memory manager.
- Parameters:
gc_threshold (float) – Trigger GC when memory usage exceeds this fraction (0-1)
safety_factor (float) – Multiply memory requirements by this factor for safety
enable_adaptive_safety (bool) – Enable adaptive safety factor reduction (1.2 -> 1.05 after warmup)
disable_padding (bool) – Disable padding/bucketing for strict memory environments (Task 5.6). When True: uses exact shapes, sets safety_factor=1.0. Use case: cloud quotas, strict memory limits.
memory_cache_ttl (float) – TTL in seconds for cached memory info (default: 1.0). Reduces psutil system call overhead by 90%.
adaptive_ttl (bool) – Enable adaptive TTL based on call frequency (default: True). High-frequency callers (>100 calls/sec) get 15s effective TTL. Medium-frequency callers (>10 calls/sec) get 10s effective TTL. Low-frequency callers use the default TTL. Reduces psutil overhead in streaming optimization by 15-20%.
- get_available_memory()[source]¶
Get available memory in bytes.
- Returns:
available – Available memory in bytes
- Return type:
Notes
Uses TTL-based caching to reduce psutil system call overhead by 90%. When adaptive_ttl is enabled, the effective TTL is adjusted based on call frequency to further reduce overhead for streaming optimization.
- get_memory_usage_bytes()[source]¶
Get current memory usage in bytes.
- Returns:
usage – Current memory usage in bytes
- Return type:
Notes
Uses TTL-based caching to reduce psutil system call overhead by 90%.
- get_memory_usage_fraction()[source]¶
Get current memory usage as fraction of total.
- Returns:
fraction – Memory usage fraction (0-1)
- Return type:
Notes
Uses TTL-based caching to reduce psutil system call overhead by 90%.
- predict_memory_requirement(n_points, n_params, algorithm='trf', dtype=<class 'jax.numpy.float64'>)[source]¶
Predict memory requirement for optimization.
- Parameters:
- Returns:
bytes_needed – Estimated memory requirement in bytes
- Return type:
Notes
Memory requirements scale linearly with precision: - float32: 4 bytes per element (50% memory savings) - float64: 8 bytes per element (default, higher precision)
- memory_guard(bytes_needed)[source]¶
Context manager to ensure memory availability.
- Parameters:
bytes_needed (int) – Required memory in bytes
- Raises:
MemoryError – If insufficient memory is available
- get_safety_telemetry()[source]¶
Get safety factor telemetry statistics.
- Returns:
telemetry – Safety factor telemetry with: - current_safety_factor: Current safety factor - initial_safety_factor: Initial safety factor (1.2) - min_safety_factor: Target minimum (1.05) - telemetry_entries: Number of telemetry entries collected - p95_safety_needed: 95th percentile of safety factors needed (if data available) - safety_factor_history: List of safety factors over time
- Return type:
- allocate_array(shape, dtype=<class 'numpy.float64'>, zero=True)[source]¶
Allocate array with memory pooling and LRU tracking.
- Parameters:
- Returns:
array – Allocated array
- Return type:
np.ndarray
- Raises:
MemoryError – If allocation fails
Notes
Task Group 7 (1.2a): Uses LRU tracking via OrderedDict. When an array is reused from the pool, it is moved to the end (most recently used) to enable proper LRU eviction.
- free_array(arr)[source]¶
Return array to pool for reuse.
- Parameters:
arr (np.ndarray) – Array to free
Notes
Task Group 7 (1.2a): Uses LRU tracking via OrderedDict. The returned array is added/moved to the end of the pool, marking it as recently used.
- get_memory_stats()[source]¶
Get memory usage statistics.
- Returns:
stats – Memory statistics including current usage, peak, pool size
- Return type:
- optimize_memory_pool(max_arrays=100)[source]¶
Optimize memory pool using LRU eviction.
- Parameters:
max_arrays (int) – Maximum number of arrays to keep in pool
Notes
Task Group 7 (1.2a): Uses LRU eviction via popitem(last=False). Arrays are evicted in order of least recent use, keeping the most recently used arrays in the pool.
- temporary_allocation(shape, dtype=<class 'numpy.float64'>)[source]¶
Context manager for temporary array allocation.