$ status available for new projects·replies within 24 hours

+91 97125 15105hello@kamleshnishad.comLinkedIn

Node.js2 Apr 2025 · 1 min read

Finding a Node.js memory leak with three heap snapshots

Cover image

The service generated PDF reports for a logistics client. Memory rose steadily from boot, and Kubernetes killed it around the six-hour mark.

Three snapshots, one comparison

Take a heap snapshot after boot, another after 1,000 requests, and a third after 2,000. Compare the second and third. Whatever grows by the same amount between them is your leak.

The comparison pointed at closures retained by an EventEmitter. Each request registered a progress listener with on() and never removed it.

// before
jobs.on("progress", report);
// after
jobs.once("done:" + id, report);

Memory now sits flat at 180 MB. We also added a check to CI that fails if any emitter passes 50 listeners.

0 comments

Join the discussion

No comments yet — be the first.