/** * SMI-1308: Memory Monitor * * Monitors memory usage and triggers cleanup when thresholds are exceeded. * Integrates with ParseCache for memory pressure handling. * * @see docs/internal/architecture/multi-language-analysis.md * @module analysis/memory-monitor */ import type { ParseCache } from './cache.js'; /** * Memory usage statistics */ export interface MemoryStats { /** Heap used in bytes */ heapUsed: number; /** Total heap size in bytes */ heapTotal: number; /** External memory in bytes */ external: number; /** ArrayBuffers memory in bytes */ arrayBuffers: number; /** Resident set size in bytes */ rss: number; /** Memory threshold in bytes */ threshold: number; /** Whether currently over threshold */ isOverThreshold: boolean; } /** * Result of a cleanup operation */ export interface CleanupResult { /** Whether cleanup was performed */ cleaned: boolean; /** Bytes freed (estimated) */ freedBytes: number; /** Cleanup reason */ reason?: string; } /** * Options for MemoryMonitor */ export interface MemoryMonitorOptions { /** Memory threshold in MB (default: 500) */ thresholdMB?: number; /** ParseCache instance for cleanup */ cache?: ParseCache; /** Enable verbose logging */ verbose?: boolean; } /** * Monitor memory usage and trigger cleanup when needed * * Provides: * - Real-time memory statistics * - Automatic cleanup when threshold exceeded * - Periodic monitoring with configurable interval * - Integration with ParseCache * * @example * ```typescript * const cache = new ParseCache({ maxMemoryMB: 200 }) * const monitor = new MemoryMonitor({ * thresholdMB: 500, * cache * }) * * // Check memory and cleanup if needed * const result = monitor.checkAndCleanup() * if (result.cleaned) { * console.log(`Freed ${MemoryMonitor.formatBytes(result.freedBytes)}`) * } * * // Start periodic monitoring * const stop = monitor.startMonitoring(10000) // 10 seconds * * // Later... * stop() * ``` */ export declare class MemoryMonitor { private readonly thresholdBytes; private readonly cache; private readonly verbose; private monitorInterval; private cleanupCount; private totalFreedBytes; constructor(options?: MemoryMonitorOptions); /** * Get current memory statistics * * @returns Current memory usage statistics * * @example * ```typescript * const stats = monitor.getStats() * console.log(`Heap: ${MemoryMonitor.formatBytes(stats.heapUsed)}`) * console.log(`Over threshold: ${stats.isOverThreshold}`) * ``` */ getStats(): MemoryStats; /** * Check memory and cleanup if needed * * Triggers cleanup when heap usage exceeds threshold. * Clears cache and requests garbage collection. * * @returns Cleanup result with bytes freed * * @example * ```typescript * const result = monitor.checkAndCleanup() * if (result.cleaned) { * console.log(`Cleaned up ${MemoryMonitor.formatBytes(result.freedBytes)}`) * } * ``` */ checkAndCleanup(): CleanupResult; /** * Force cleanup regardless of threshold * * Useful for explicit memory management before large operations. * * @returns Cleanup result with bytes freed */ forceCleanup(): CleanupResult; /** * Start periodic monitoring * * Checks memory at the specified interval and performs * cleanup when threshold is exceeded. * * @param intervalMs - Monitoring interval in milliseconds (default: 10000) * @returns Stop function to cancel monitoring * * @example * ```typescript * const stop = monitor.startMonitoring(5000) * * // Later, when done... * stop() * ``` */ startMonitoring(intervalMs?: number): () => void; /** * Stop periodic monitoring */ stopMonitoring(): void; /** * Get cleanup count * * @returns Number of cleanups performed */ getCleanupCount(): number; /** * Get total bytes freed across all cleanups * * @returns Total bytes freed (estimated) */ getTotalFreedBytes(): number; /** * Get monitoring status * * @returns Whether periodic monitoring is active */ isMonitoring(): boolean; /** * Get memory threshold * * @returns Threshold in bytes */ getThreshold(): number; /** * Reset statistics */ resetStats(): void; /** * Format bytes for display * * @param bytes - Number of bytes * @returns Human-readable string * * @example * ```typescript * MemoryMonitor.formatBytes(1024) // "1.00 KB" * MemoryMonitor.formatBytes(1048576) // "1.00 MB" * MemoryMonitor.formatBytes(1073741824) // "1.00 GB" * ``` */ static formatBytes(bytes: number): string; /** * Get memory usage summary string * * @returns Formatted summary of current memory usage */ getSummary(): string; } //# sourceMappingURL=memory-monitor.d.ts.map