📎 Webclip
Causes of Memory Leaks in JavaScript and How to Avoid Them
The article says memory leaks matter more in SPAs because growing JavaScript memory use can hurt performance and even crash the browser tab. It defines a leak as an object that stays reachable through an unintended reference, and points to Task Manager, Performance, and Memory tools as ways to detect it.
Reading notes#
- In older page-based websites, memory is usually cleared as the user moves between pages, but SPAs need more care with JavaScript memory management.
- A memory leak happens when an object that should be collected stays reachable from the root through an unintended reference.
- Browser Task Managers can show the JavaScript memory footprint of each tab, and steady growth while the page is idle can signal a leak.
- Developer Tools can reveal leak patterns through performance recordings and heap snapshots.
- Accidental global variables stay reachable from the root and are not garbage collected; strict mode helps prevent this.
- Closures keep referenced variables alive after the outer function has finished, so their lifespan and retained objects need attention.
- Timers can keep objects in memory through callback references until the timer is cleared.
- Event listeners retain captured variables until they are removed or the DOM element is removed; anonymous listeners are harder to clean up.
- Using
{once: true}makes an event listener remove itself after running once. - A cache can grow without limit if unused entries are never removed;
WeakMapcan let entries disappear when the key is no longer referenced. - A DOM node can remain in memory after removal from the document if JavaScript still holds a direct reference to it.
- Moving DOM references into local scope can help avoid detached elements lingering in memory.
