Memory

Memory Monitor

Periodic heap monitoring in AxilJS with leak detection, V8 heap-limit thresholds, an optional absolute heap cap and scan events.

2 min readUpdated DocumentationEdit this page
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

OptionDefaultDescription
intervalMs30000Scan interval
warnThresholdPercent70Heap usage (percent of the V8 heap limit) that emits warning
criticalThresholdPercent85Heap usage (percent of the V8 heap limit) that marks the scan critical
maxHeapMbnot setAbsolute cap: heapUsedMb at or above it is treated as critical
autoFixtrueLet leak detection prune overflowing tracked caches
verbosefalseLog every scan, not only scans with issues
onLeaknoneCalled with the LeakRecord[] when leaks are found
onCriticalnoneCalled 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?

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License · Built by SyntaxilitY