Appearance
Memory and Garbage Collection
30. What is garbage collection in Node.js?
The short answer
Garbage collection is V8 automatically finding memory that your program can no longer use and freeing it, so you never call free yourself. An object is kept alive as long as it can be reached by following references from a root, such as a global variable, the current call stack or an active closure. Anything unreachable is garbage. The collector runs from time to time, mostly in the background, but some steps briefly pause your JavaScript, which shows up as latency spikes when the heap is large or under pressure.
Reachability, with an example:
ts
const recentOrders: Order[] = [] // (1) a module-level root
function handleOrder(order: Order) {
const receipt = renderReceipt(order) // (2)
recentOrders.push(order) // (3)
return receipt
}- A module-level array is reachable for the whole life of the process.
receiptis only reachable whilehandleOrderruns, and while the caller holds it. Afterwards it becomes garbage and is freed.- Each order pushed into the array stays reachable for ever. Nothing is freed, and memory grows with every order: a leak (question 32).
What "garbage" does not mean: "not used recently". The collector cannot know whether you will use an object again. It only knows whether you still can. A cache entry you will never read again, but that is still in a map, is not garbage.
Seeing memory use:
ts
const m = process.memoryUsage()
console.log({
rssMb: Math.round(m.rss / 1e6), // all memory the process uses
heapUsedMb: Math.round(m.heapUsed / 1e6), // live JavaScript objects
heapTotalMb: Math.round(m.heapTotal / 1e6),
externalMb: Math.round(m.external / 1e6), // memory outside the heap, such as Buffers
})Pauses matter for latency. A full collection of a large, crowded heap can stop JavaScript for tens or hundreds of milliseconds. Keeping the heap small and reducing short-lived allocations in hot code paths reduces both the frequency and length of pauses.
31. How does V8 manage memory?
The short answer
V8 splits its heap into generations, because most objects die young. New objects go into the young generation, a small area cleaned very often by a fast copying collector, the scavenger: it copies the few surviving objects out and discards the rest. Objects that survive twice are promoted to the old generation, which is larger and cleaned less often by mark-sweep-compact: mark everything reachable, sweep away the rest, and compact to reduce gaps. Most of this work runs concurrently or in small steps, to keep pauses short.
Why two generations work. In a web server, almost everything created for a request, such as parsed JSON, temporary strings and arrays, is garbage a few milliseconds later. Cleaning a small young area often is very cheap, because only the few survivors are copied. Long-lived data, such as caches and connection pools, settles in the old generation, which is checked less often.
The heap limit and out-of-memory crashes. The old generation has a size limit, decided by Node.js from the memory available. When live objects fill it and collection cannot free enough, the process crashes with "JavaScript heap out of memory".
bash
node --max-old-space-size=1536 dist/server.js # allow 1.5 GB of old generationIn containers, set this explicitly to roughly 75% of the container's memory limit. The rest is needed for Buffers, native memory and the young generation. If the container limit is reached first, the operating system kills the process with no JavaScript error at all.
Memory outside the heap. Buffer data, used for files, network data and images, lives outside the V8 heap and appears as external and arrayBuffers in process.memoryUsage(). A process can run out of memory from Buffers while the heap looks small (question 45).
32. What are common causes of memory leaks in Node.js?
The short answer
A memory leak is memory that stays reachable although the program no longer needs it, so the heap grows until the process slows down or crashes. The common causes are unbounded caches and maps that only ever grow, event listeners added on every request and never removed, timers and intervals never cleared, closures that keep large objects alive, global arrays used to "keep track" of things, queues that fill faster than they drain, and in-memory session stores in production.
| Cause | Typical code | Fix |
|---|---|---|
| Unbounded cache | cache.set(userId, profile) with no limit | LRU cache with a maximum size or expiry (question 102) |
| Listener per request | emitter.on('x', …) inside a handler | once, off, or an AbortSignal |
| Timers | setInterval started per connection, never cleared | clearInterval on close |
| Closures holding big objects | A callback that captures a whole request body | Capture only the fields you need (question 14) |
| Global collections | const seen = [] at module level | A bounded structure, or a database |
| Growing queues | Producers faster than consumers, in memory | Backpressure, limits, an external queue |
| In-memory sessions | express-session default MemoryStore | Redis or another shared store |
| Promises that never settle | await on a request with no timeout | Timeouts with AbortSignal (question 29) |
A realistic leak, a per-user cache:
ts
const pricesByUser = new Map<string, PriceList>() // (1)
app.get('/prices', async (req, res) => {
let prices = pricesByUser.get(req.user.id)
if (!prices) {
prices = await buildPriceList(req.user.id) // 200 KB each
pricesByUser.set(req.user.id, prices) // (2)
}
res.json(prices)
})- A map that lives for the whole process.
- One entry per user, never removed. With 50,000 users visiting over a week, that is 10 GB. The process is restarted by out-of-memory kills every few days, and nobody can see why.
The fix: a bounded cache with an expiry, such as lru-cache with max: 5000, ttl: 10 * 60_000, or move it to Redis where memory is managed separately.
Not every growth is a leak. Memory rising after start-up and then levelling off is warm-up. A leak is a steady rise that never levels off under steady traffic.
33. How would you investigate and fix a memory leak?
The short answer
First confirm it is a leak: graph heap used over time under steady traffic and look for a steady climb that never comes back down after garbage collection. Reproduce it locally or in staging with a load generator. Then take heap snapshots, one after warm-up and others after more traffic, and compare them in Chrome DevTools to see which object types keep growing. Follow the retainer chain of those objects back to the code that holds them. Fix it, usually by bounding, removing or not capturing something, and confirm the graph is flat.
Step by step:
- Confirm. In your metrics, watch
heapUsedafter collections. A saw-tooth around a flat line is healthy. Low points that climb steadily over hours are a leak. - Reproduce. Run the service in staging with steady traffic from a load generator such as autocannon or k6, hitting the endpoints that were busy in production.
- Take snapshots.
bash
node --inspect dist/server.js # then open chrome://inspect, Memory tab
node --heapsnapshot-signal=SIGUSR2 dist/server.js # kill -USR2 <pid> writes a snapshot filets
import { writeHeapSnapshot } from 'node:v8'
app.post('/internal/heap-snapshot', requireAdmin, (_req, res) => res.json({ file: writeHeapSnapshot() }))Take snapshot A after warm-up, run traffic for a few minutes, take snapshot B, then C.
- Compare. Load them in Chrome DevTools, Memory tab, choose the Comparison view, and sort by size delta. Look for a constructor whose count rises between every snapshot, such as
PriceList,Objectwith a familiar shape,Array,Closureor listener functions. - Follow the retainers. Select one instance; the Retainers panel shows the chain keeping it alive, for example
Map→pricesByUserinprices.ts. That names the leaking code. - Fix and verify. Bound the map, remove the listener, clear the timer. Run the same load again and confirm a flat graph.
Snapshots have a cost. Writing one pauses the process and needs about as much extra memory as the heap itself. Take them on one instance taken out of the load balancer, not on every production pod at once.
For crashes you cannot reproduce, start with --heapsnapshot-near-heap-limit=2, which writes a snapshot automatically just before an out-of-memory crash, so you can study it afterwards.
When the heap is fine but RSS grows, look outside V8: Buffers that are retained, native add-ons, or memory fragmentation. Compare rss, heapUsed and external over time to know which one is growing.