Memory
Memory Monitor
Periodic heap monitoring in AxilJS with leak detection, V8 heap-limit thresholds, an optional absolute heap cap and scan events.
On this page4
Usage
TypeScript
TS
1import { MemoryMonitor } from '@axiljs/memory'
2
3const monitor = new MemoryMonitor({
4 intervalMs: 30000,
5 warnThresholdPercent: 70,
6 criticalThresholdPercent: 85,
7 autoFix: true,
8 onLeak: (leaks) => logger.warn('Leaks', { count: leaks.length }),
9 onCritical: (snap) => logger.error('Critical', { mb: snap.heapUsedMb })
10})
11
12monitor.start()
13// later
14monitor.stop()Options
| Option | Default | Description |
|---|---|---|
intervalMs | 30000 | Scan interval |
warnThresholdPercent | 70 | Heap usage (percent of the V8 heap limit) that emits warning |
criticalThresholdPercent | 85 | Heap usage (percent of the V8 heap limit) that marks the scan critical |
maxHeapMb | not set | Absolute cap: heapUsedMb at or above it is treated as critical |
autoFix | true | Let leak detection prune overflowing tracked caches |
verbose | false | Log every scan, not only scans with issues |
onLeak | none | Called with the LeakRecord[] when leaks are found |
onCritical | none | Called with the MemorySnapshot on critical |
On critical, the monitor also runs optimizer.optimize().
Manual scan
TypeScript
TS
1const report = await monitor.scan()
2report.status // 'clean' | 'leaks-fixed' | 'leaks-detected' | 'critical'
3report.freedMb
4report.leaks // LeakRecord[]
5
6monitor.getLatestReport()
7monitor.getReports()
8monitor.getCurrentSnapshot()Events
MemoryMonitor is an EventEmitter:
TypeScript
TS
1monitor.on('scan', (report) => {})
2monitor.on('leak', (leaks) => {})
3monitor.on('warning', (snapshot) => {})
4monitor.on('critical', (snapshot) => {})Was this page helpful?