/** * @file Memory Optimization Utilities * @description Memory leak prevention, garbage collection optimization, * and memory pressure handling for React applications. * * Features: * - Memory leak detection * - Weak reference management * - Object pool management * - Cache eviction strategies * - Memory pressure monitoring * - Cleanup orchestration */ /** * Memory pressure level */ export type MemoryPressureLevel = 'none' | 'moderate' | 'critical'; /** * Memory statistics */ export interface MemoryStats { usedJSHeapSize: number; totalJSHeapSize: number; jsHeapSizeLimit: number; usagePercent: number; pressureLevel: MemoryPressureLevel; timestamp: number; } /** * Memory leak suspect */ export interface MemoryLeakSuspect { type: string; count: number; estimatedSize: number; growthRate: number; firstSeen: number; lastSeen: number; stackTrace?: string; } /** * Object pool configuration */ export interface ObjectPoolConfig { /** Factory function to create new objects */ create: () => T; /** Reset function to clear object state */ reset: (obj: T) => void; /** Maximum pool size */ maxSize: number; /** Minimum objects to keep in pool */ minSize: number; /** Auto-shrink pool when idle */ autoShrink: boolean; /** Shrink interval in ms */ shrinkInterval: number; } /** * Cache configuration */ export interface CacheConfig { /** Maximum number of entries */ maxEntries: number; /** Maximum memory size in bytes */ maxMemorySize: number; /** Time-to-live in ms */ ttl: number; /** Eviction policy */ evictionPolicy: 'lru' | 'lfu' | 'fifo'; /** Enable memory pressure response */ memoryPressureAware: boolean; } /** * Cleanup callback */ export type CleanupCallback = () => void | Promise; /** * Memory pressure callback */ export type MemoryPressureCallback = (level: MemoryPressureLevel) => void; /** * Memory monitoring and pressure detection */ export declare class MemoryMonitor { private pressureCallbacks; private lastPressureLevel; private monitorInterval; private history; private maxHistorySize; /** * Start monitoring memory */ start(intervalMs?: number): void; /** * Stop monitoring */ stop(): void; /** * Get current memory stats */ getStats(): MemoryStats | null; /** * Get memory history */ getHistory(): MemoryStats[]; /** * Subscribe to memory pressure changes */ onPressureChange(callback: MemoryPressureCallback): () => void; /** * Force garbage collection (if available) */ forceGC(): boolean; private getMemoryInfo; private checkMemory; private calculatePressureLevel; private notifyPressureChange; } /** * Generic object pool for reducing GC pressure */ export declare class ObjectPool { private config; private pool; private activeCount; private shrinkTimer; constructor(config: ObjectPoolConfig); /** * Acquire an object from the pool */ acquire(): T; /** * Release an object back to the pool */ release(obj: T): void; /** * Get pool statistics */ getStats(): { poolSize: number; activeCount: number; maxSize: number; }; /** * Clear the pool */ clear(): void; /** * Destroy the pool */ destroy(): void; private startAutoShrink; private shrink; } /** * Cache with memory pressure awareness and multiple eviction strategies */ export declare class MemoryAwareCache { private config; private cache; private currentMemorySize; private memoryMonitor; constructor(config?: Partial); /** * Get a value from cache */ get(key: K): V | undefined; /** * Set a value in cache */ set(key: K, value: V, sizeEstimate?: number): void; /** * Delete a value from cache */ delete(key: K): boolean; /** * Check if key exists */ has(key: K): boolean; /** * Clear the cache */ clear(): void; /** * Get cache statistics */ getStats(): { size: number; memorySize: number; maxEntries: number; maxMemorySize: number; }; /** * Cleanup expired entries */ cleanup(): number; /** * Destroy the cache and cleanup resources */ destroy(): void; private setupMemoryPressureResponse; private evictOne; private selectEvictionCandidate; private selectLRUCandidate; private selectLFUCandidate; private selectFIFOCandidate; private estimateSize; } /** * Orchestrates cleanup across multiple resources */ export declare class CleanupOrchestrator { private cleanupCallbacks; private isDestroyed; /** * Register a cleanup callback */ register(id: string, callback: CleanupCallback): () => void; /** * Unregister a cleanup callback */ unregister(id: string): void; /** * Run a specific cleanup */ cleanup(id: string): Promise; /** * Run all cleanups */ cleanupAll(): Promise; /** * Destroy the orchestrator and run all cleanups */ destroy(): Promise; } /** * Manages weak references with automatic cleanup */ export declare class WeakRefManager { private refs; private finalizationRegistry; private cleanupCallbacks; constructor(); /** * Add a weak reference */ set(key: string, value: T, onCleanup?: () => void): void; /** * Get a weak reference (may return undefined if collected) */ get(key: string): T | undefined; /** * Check if reference exists and is alive */ has(key: string): boolean; /** * Delete a reference */ delete(key: string): boolean; /** * Get all live references */ getLive(): Map; /** * Clean up dead references */ prune(): number; } /** * Detects potential memory leaks by tracking object allocations */ export declare class MemoryLeakDetector { private tracking; private interval; private growthThreshold; /** * Start tracking a category of objects */ track(category: string, count: number): void; /** * Get potential memory leak suspects */ getSuspects(): MemoryLeakSuspect[]; /** * Clear tracking data */ clear(): void; /** * Start automatic detection */ startAutoDetection(intervalMs?: number): void; /** * Stop automatic detection */ stopAutoDetection(): void; } /** * Get the global memory monitor instance */ export declare function getMemoryMonitor(): MemoryMonitor; /** * Get the global cleanup orchestrator instance */ export declare function getCleanupOrchestrator(): CleanupOrchestrator;