Skip to content

Interview PrepMemory and debugging · Advanced

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
}
  1. A module-level array is reachable for the whole life of the process.
  2. receipt is only reachable while handleOrder runs, and while the caller holds it. Afterwards it becomes garbage and is freed.
  3. 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.

young generationnew objects land heresmall, collected oftenscavenger: copy survivorsold generationlong-lived objectslarge, collected less oftenmark, sweep, compactlarge objectsbig arrays,stringssurvivedtwiceoutside the V8 heap: Buffers, native memory from add-ons, thread stacks
Most objects die young. New objects start in a small, frequently cleaned area. The few that keep surviving move to the old generation, which is cleaned less often but more thoroughly. Buffers are stored outside the heap altogether.

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 generation

In 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.

CauseTypical codeFix
Unbounded cachecache.set(userId, profile) with no limitLRU cache with a maximum size or expiry (question 102)
Listener per requestemitter.on('x', …) inside a handleronce, off, or an AbortSignal
TimerssetInterval started per connection, never clearedclearInterval on close
Closures holding big objectsA callback that captures a whole request bodyCapture only the fields you need (question 14)
Global collectionsconst seen = [] at module levelA bounded structure, or a database
Growing queuesProducers faster than consumers, in memoryBackpressure, limits, an external queue
In-memory sessionsexpress-session default MemoryStoreRedis or another shared store
Promises that never settleawait on a request with no timeoutTimeouts 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)
})
  1. A map that lives for the whole process.
  2. 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.

① confirmheap climbs,never recovers② reproducesteady loadin staging③ snapshotsA after warm-up,B after traffic④ comparewhat grew?who retains it?⑤ fixthen confirma flat graphleak: keeps risinghealthy: saw-tooth, flat
Prove it, catch it, follow it. A healthy heap goes up and down around a flat line. A leak's low points keep rising. Two snapshots taken minutes apart show which objects grew, and their retainers lead to the code.

Step by step:

  1. Confirm. In your metrics, watch heapUsed after collections. A saw-tooth around a flat line is healthy. Low points that climb steadily over hours are a leak.
  2. 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.
  3. 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 file
ts
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.

  1. 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, Object with a familiar shape, Array, Closure or listener functions.
  2. Follow the retainers. Select one instance; the Retainers panel shows the chain keeping it alive, for example Map → pricesByUser in prices.ts. That names the leaking code.
  3. 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.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
Node.js
Runtime
Memory and debugging
Data and files
Process and packages
Other topics
Nothing picked yet