Memory
Leak Detection
Detect common Node.js memory leaks in AxilJS — listener build-up, tracked timers, cache overflow and global pollution — with automatic cache pruning.
What is detected
detectAndFix(autoFix = true) runs these checks (the MemoryMonitor calls it on every scan):
| Check | Reported as | Auto fix |
|---|---|---|
Tracked emitter has more listeners than its getMaxListeners() | event-listener | Warns |
More than 3 listeners on a process event (uncaughtException, unhandledRejection, exit, SIGINT, SIGTERM) | event-listener | Warns |
| More than 100 tracked timers / 50 tracked intervals still active | timer / interval | Warns |
Tracked cache larger than its maxSize | cache-overflow | Prunes oldest entries |
Large unknown values on global | large-string | Warns |
Track resources
TypeScript
TS
1import { trackTimer, trackInterval, trackCache, trackEmitter, untrackTimer } from '@axiljs/memory'
2
3const timer = trackTimer(setTimeout(work, 5000))
4const tick = trackInterval(setInterval(poll, 1000))
5const cache = trackCache('sessions', new Map(), 10000) // maxSize defaults to 10000
6trackEmitter(bus, 'EventBus')
7
8clearTimeout(timer)
9untrackTimer(timer)Run a scan manually
TypeScript
TS
import { detectAndFix } from '@axiljs/memory'
const leaks = await detectAndFix(true) // pass false to report without pruningPer-request
TypeScript
TS
import { memoryMiddleware } from '@axiljs/memory'
app.use(memoryMiddleware({ leakThresholdMb: 50, exposeHeader: true, logFlagged: true }))| Option | Default | Description |
|---|---|---|
leakThresholdMb | 50 | Log requests whose heap delta exceeds this many MB |
exposeHeader | false | Add X-Memory-Usage (and X-Memory-Delta when positive) response headers |
logFlagged | true | Log flagged requests to the console |
Was this page helpful?