| 549 | * Memory monitor for tracking usage during operations |
| 550 | */ |
| 551 | export class MemoryMonitor { |
| 552 | private checkInterval: ReturnType<typeof setInterval> | null = null; |
| 553 | private peakUsage = 0; |
| 554 | private threshold: number; |
| 555 | private onThresholdExceeded?: (usage: number) => void; |
| 556 | |
| 557 | constructor( |
| 558 | thresholdMB: number = 500, |
| 559 | onThresholdExceeded?: (usage: number) => void |
| 560 | ) { |
| 561 | this.threshold = thresholdMB * 1024 * 1024; |
| 562 | this.onThresholdExceeded = onThresholdExceeded; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Start monitoring memory usage |
| 567 | */ |
| 568 | start(intervalMs: number = 1000): void { |
| 569 | this.stop(); |
| 570 | this.peakUsage = 0; |
| 571 | |
| 572 | this.checkInterval = setInterval(() => { |
| 573 | const usage = process.memoryUsage().heapUsed; |
| 574 | if (usage > this.peakUsage) { |
| 575 | this.peakUsage = usage; |
| 576 | } |
| 577 | if (usage > this.threshold && this.onThresholdExceeded) { |
| 578 | this.onThresholdExceeded(usage); |
| 579 | } |
| 580 | }, intervalMs); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Stop monitoring |
| 585 | */ |
| 586 | stop(): void { |
| 587 | if (this.checkInterval) { |
| 588 | clearInterval(this.checkInterval); |
| 589 | this.checkInterval = null; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Get peak memory usage in bytes |
| 595 | */ |
| 596 | getPeakUsage(): number { |
| 597 | return this.peakUsage; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Get current memory usage in bytes |
| 602 | */ |
| 603 | getCurrentUsage(): number { |
| 604 | return process.memoryUsage().heapUsed; |
| 605 | } |
| 606 | } |
nothing calls this directly
no outgoing calls
no test coverage detected