CodeZettaInterview Hub

JavaScript · Memory Management · Mid-Level · Debugging

What are common memory leak patterns in browser JavaScript?

Short Interview Answer

Detached DOM retained by JS refs, forgotten listeners/timers, unbounded caches, global accumulators, and closures holding large graphs.

Detailed Explanation

Classic leaks: store DOM nodes in arrays and remove from document without clearing refs; `addEventListener` without remove on teardown; `setInterval` forever; growing Maps/objects as caches without eviction; module-level arrays appending forever; console retaining objects in DevTools. SPAs leak across route changes when subscriptions aren't disposed. Fix with cleanup in unmount effects, WeakMap/WeakRef where appropriate, LRU caches, and heap snapshot comparisons.

Example

const cache = [];
function leak(node) {
  cache.push(node); // keeps detached nodes alive
}

Interview Tip

Walk through detached DOM — still the most cited example.

Common Mistake

Blaming the GC as 'broken' when application code retains references.

Did you know this answer?