/** * Memory Monitoring Utilities * * Provides utilities for monitoring memory usage during indexing operations. * Helps detect and prevent OOM conditions by providing early warnings and * graceful degradation capabilities (MCP-22). */ /** * Memory usage statistics */ export interface MemoryStats { /** Heap memory used in bytes */ heapUsed: number; /** Total heap memory allocated in bytes */ heapTotal: number; /** Resident set size (total memory allocated) in bytes */ rss: number; /** External memory used by C++ objects bound to JavaScript */ external: number; /** Percentage of heap used (heapUsed / heapTotal) */ heapUsedPercent: number; } /** * Memory warning level */ export type MemoryWarningLevel = 'normal' | 'warning' | 'critical'; /** * Memory status with warning information */ export interface MemoryStatus { /** Current memory statistics */ stats: MemoryStats; /** Warning level based on current usage */ level: MemoryWarningLevel; /** Human-readable message about current status */ message: string; } /** * Callback for memory warnings */ export type MemoryWarningCallback = (status: MemoryStatus) => void; /** * Warning threshold - warn when heap usage exceeds this percentage */ export declare const MEMORY_WARNING_THRESHOLD = 0.7; /** * High memory threshold - switch to streaming mode when heap usage exceeds this * At 80%, we have enough memory pressure to warrant slower but safer indexing */ export declare const MEMORY_HIGH_THRESHOLD = 0.8; /** * Critical threshold - take action when heap usage exceeds this percentage * Note: Set to 90% to allow headroom after embedding model loads (~300MB) */ export declare const MEMORY_CRITICAL_THRESHOLD = 0.9; /** * Minimum heap size (in bytes) before percentage-based critical checks apply. * V8's heapTotal is dynamic and starts small. We shouldn't consider memory * "critical" if the heap is small and V8 can still expand it. * 256MB is a reasonable minimum - below this, V8 can easily expand. */ export declare const MIN_HEAP_FOR_CRITICAL_CHECK: number; /** * Absolute memory threshold (in bytes) - always trigger critical if heapUsed exceeds this. * This is a safety net for very large heaps. */ export declare const ABSOLUTE_CRITICAL_THRESHOLD: number; /** * Default interval for periodic memory checks (in milliseconds) */ export declare const DEFAULT_CHECK_INTERVAL = 5000; /** * Check if memory critical checks are disabled via environment variable. * Set SEARCH_MCP_DISABLE_MEMORY_CRITICAL=true to disable critical memory checks. * This is useful for testing where memory pressure is expected and safe. */ export declare function isMemoryCriticalCheckDisabled(): boolean; /** * Get current memory usage statistics * * @returns Current memory statistics */ export declare function getMemoryStats(): MemoryStats; /** * Format bytes to human-readable string * * @param bytes - Number of bytes * @returns Human-readable string (e.g., "256MB") */ export declare function formatBytes(bytes: number): string; /** * Get current memory status with warning level * * @returns Memory status with warning information */ export declare function getMemoryStatus(): MemoryStatus; /** * Log current memory usage * * @param phase - Description of the current operation phase */ export declare function logMemoryUsage(phase: string): void; /** * Check if memory usage is at a critical level. * * This uses smart thresholds that account for V8's dynamic heap sizing: * - Disabled in test environments (VITEST=true, NODE_ENV=test) * - Disabled via SEARCH_MCP_DISABLE_MEMORY_CRITICAL=true * - Only triggers percentage-based check if heapTotal > MIN_HEAP_FOR_CRITICAL_CHECK * - Always triggers if heapUsed > ABSOLUTE_CRITICAL_THRESHOLD (safety net) * * @returns true if memory usage is critical and action should be taken */ export declare function isMemoryCritical(): boolean; /** * Check if memory usage is at warning or critical level * * @returns true if memory usage requires attention */ export declare function isMemoryWarning(): boolean; /** * Check if memory usage is high enough to warrant streaming mode. * * When memory is above 80%, we should use streaming mode (process fewer files * at a time and write to DB immediately) to avoid accumulating too much data * in memory before writing. * * This is a softer check than isMemoryCritical() - it doesn't block indexing, * just switches to a more memory-efficient (but slower) mode. * * @returns true if memory usage is high and streaming mode should be used */ export declare function isMemoryHigh(): boolean; /** * Request garbage collection if available * * Note: This only works if Node.js is started with --expose-gc flag. * In production, V8 manages GC automatically, so this is mainly useful * for testing and debugging. * * @returns true if GC was requested, false if not available */ export declare function requestGarbageCollection(): boolean; /** * Force garbage collection and wait for memory to be released. * This is useful between test runs to ensure clean state. * * If global.gc is not available (no --expose-gc flag), this function * still works by clearing module caches and waiting for natural GC. * * @param delayMs - Time to wait after GC request (default 100ms) * @returns Promise that resolves when cleanup is complete */ export declare function forceGarbageCollection(delayMs?: number): Promise; /** * Get current memory usage in MB (useful for logging) * * @returns Current heap used in MB */ export declare function getMemoryUsageMB(): number; /** * Memory monitor that periodically checks memory usage and triggers * callbacks when thresholds are exceeded. * * @example * ```typescript * const monitor = new MemoryMonitor(); * monitor.onWarning((status) => { * console.log('Memory warning:', status.message); * }); * monitor.start(); * // ... do work ... * monitor.stop(); * ``` */ export declare class MemoryMonitor { private intervalId; private warningCallbacks; private checkInterval; private lastWarningLevel; /** * Create a new memory monitor * * @param checkInterval - Interval between checks in milliseconds */ constructor(checkInterval?: number); /** * Register a callback for memory warnings * * @param callback - Function to call when memory reaches warning or critical level */ onWarning(callback: MemoryWarningCallback): void; /** * Start periodic memory monitoring */ start(): void; /** * Stop periodic memory monitoring */ stop(): void; /** * Perform a single memory check */ check(): void; /** * Check if the monitor is currently running */ isRunning(): boolean; } /** * Calculate an adaptive batch size based on current memory usage. * Reduces batch size when memory is constrained to prevent OOM. * * @param defaultBatchSize - The default batch size to use when memory is normal * @param minBatchSize - Minimum batch size to use even under memory pressure * @returns Adjusted batch size based on current memory status */ export declare function getAdaptiveBatchSize(defaultBatchSize: number, minBatchSize?: number): number; //# sourceMappingURL=memory.d.ts.map