JavaScriptMedium
How to detect a memory leak in Chrome?
Use Chrome DevTools' Memory tools: reproduce the suspected action several times, take heap snapshots before and after, compare them to find objects that keep accumulating, and inspect the retainers chain that keeps them alive.
Step-by-step workflow
- Spot the symptom: Task Manager (Shift+Esc) or the Performance monitor panel shows JS heap size or DOM node count rising steadily while you repeat an action (open and close a modal, navigate between routes).
- Record a Performance trace with "Memory" checked: a healthy app shows a saw-tooth pattern (memory rises, then GC drops it). A staircase that never comes down suggests a leak.
- Heap snapshots (Memory panel):
- Take snapshot 1 (DevTools runs garbage collection first).
- Repeat the action 5-10 times.
- Take snapshot 2, choose the Comparison view, sort by "# Delta" or "Size Delta".
- Look for constructors whose count grows with each repetition: your components, arrays, closures, "Detached" DOM elements.
- Find the cause: select a leaked object and read the Retainers panel bottom-up. It shows the path from a GC root (an event listener, a global, a timer, a cache) to the object.
- Allocation instrumentation on timeline: shows which allocations survive; blue bars that never turn grey are retained.
- Fix, then repeat the same test to confirm the count returns to baseline.
Usual culprits
Listeners on window/document not removed, intervals not cleared, unbounded caches, detached DOM nodes held in arrays or closures, subscriptions without unsubscribe, and global stores that keep growing.
Tips
Test in an incognito window (extensions add noise), filter snapshots by your component names, and use queryObjects(Constructor) in the console to count live instances.
Code example
js
1// A deliberately leaky page to practise on. Run it, then open DevTools > Memory.2document.body.innerHTML = `<button id="open">Open + close modal x10</button><p id="status"></p>`;3const leaked = []; // the "cache" that retains detached nodes45class Modal {6 constructor() {7 this.el = document.createElement("div");8 this.el.textContent = "Modal content " + "x".repeat(10_000);9 this.onResize = () => this.el.style.width = innerWidth / 2 + "px";10 window.addEventListener("resize", this.onResize); // LEAK 1: never removed11 leaked.push(this.el); // LEAK 2: keeps the element after it is removed (a "Detached" node)12 }13 open() {14 document.body.append(this.el);15 }16 close() {17 this.el.remove();18 // FIX: window.removeEventListener("resize", this.onResize); and don't store the element19 }20}2122document.getElementById("open").addEventListener("click", () => {23 for (let i = 0; i < 10; i++) {24 const m = new Modal();25 m.open();26 m.close();27 }28 document.getElementById("status").textContent = `Detached nodes kept alive: ${leaked.length}`;29});3031document.getElementById("open").click();32console.log("detached modals retained:", leaked.length); // 10 - grows on every click33// In DevTools: snapshot -> click a few times -> snapshot -> Comparison -> filter "Detached" or "Modal"34// In the Console: queryObjects(Modal) counts live instances.
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.