Diagnosing a Node.js Memory Leak Hidden in Module-Scope State
A developer's Node.js service stayed healthy for about an hour after each deploy and then steadily consumed more memory, with restarts as the only fix. The article argues that such leaks usually sit at module scope, where references outlive individual requests, rather than inside request handlers. It recommends tracking heapUsed after forced garbage collection, since a rising baseline indicates true retention, and using heap snapshots with the DevTools Comparison and Retainers views to trace the cause. The author traced the problem to a Map declared outside a function, and the piece notes that serverless platforms reusing function instances can exhibit the same behavior. It also outlines five common module-scope patterns behind most Node.js leaks, including unbounded caches, accumulating event listeners, and timers capturing request objects.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in