How to find and fix JavaScript memory leaks

What memory leaks look like in web apps, the most common causes, how to find them with browser developer tools, and patterns that prevent them.

By the UK Technology Hub teamPublished 4 minute read

The short answer

A memory leak happens when an application keeps references to data it no longer needs, so memory grows until the page slows down or crashes. In JavaScript, common causes are event listeners and timers that aren't removed, closures holding large objects, detached DOM nodes, unbounded caches and effects without cleanup in frameworks like React. Use browser memory tools to find what's growing and add proper cleanup.

JavaScript manages memory automatically through garbage collection, but it can only free objects nothing refers to anymore. If your code keeps references accidentally, memory keeps growing. In long-running single-page apps, dashboards and kiosk screens, leaks gradually make everything slower until the tab crashes.

Symptoms

  • The page gets slower the longer it's open.
  • Memory use in the browser's task manager keeps rising.
  • Animations and scrolling become janky over time.
  • Tabs crash or reload unexpectedly, especially on mobile.

Common causes

Event listeners not removed

Adding listeners to window, document or long-lived elements without removing them when components are destroyed keeps the components in memory.

Fix: remove listeners when no longer needed, or use AbortController signals to remove groups of listeners at once.

Timers and intervals

setInterval and repeated setTimeout calls keep running and holding references.

Fix: clear intervals and timeouts when components unmount or tasks complete.

Closures holding large data

Functions that capture large objects, such as datasets or DOM nodes, keep them alive as long as the function exists.

Fix: avoid capturing more than needed, and release references when done.

Detached DOM nodes

Elements removed from the page but still referenced in JavaScript, for example in arrays or maps, can't be collected.

Fix: remove references when removing elements; use WeakMap or WeakRef where appropriate.

Unbounded caches

Caches that grow forever, such as storing every API response, eventually consume too much memory.

Fix: limit cache size, expire old entries or use a least-recently-used strategy.

Framework effects without cleanup

In React, effects that subscribe to events, sockets or timers must return a cleanup function. Similar patterns exist in Vue, Angular and other frameworks.

Fix: always clean up subscriptions in effect cleanup or component destroy hooks.

Third-party libraries

Charts, maps, editors and analytics widgets may need explicit destroy calls when removed.

Fix: read library documentation and call their teardown methods.

Finding leaks with browser tools

Chrome DevTools Memory panel offers:

  1. Heap snapshots: take one, perform an action several times (such as opening and closing a modal), take another and compare. Objects that keep increasing are suspects.
  2. Allocation timelines: show where memory is being allocated over time.
  3. Detached elements: filter snapshots to find DOM nodes no longer in the page.

Performance panel with memory recording shows whether memory returns to a baseline after garbage collection.

A practical debugging routine

  1. Identify a user flow that might leak.
  2. Repeat it many times.
  3. Force garbage collection in DevTools.
  4. Compare heap snapshots.
  5. Follow retainer paths to see what's keeping objects alive.
  6. Fix and repeat to confirm memory stabilises.

Prevention patterns

  • Pair every subscription with cleanup.
  • Prefer framework-provided lifecycle hooks.
  • Use AbortController for fetch requests and listeners.
  • Keep global state small and intentional.
  • Add long-running tests for dashboards and kiosks.

Effects that run differently on server and client can also cause hydration problems; see our guide to fixing React hydration errors. Pages that jump while loading have a different cause; see our guide to fixing layout shift.

Memory leaks in long-running dashboards

Dashboards left open all day on office screens are especially prone to leaks: charts redrawn every minute, polling requests and accumulating data. Limit how much history is kept in memory, reuse chart instances rather than recreating them, and consider reloading the page automatically overnight as a safety net, while still fixing root causes.

Server-side JavaScript

Node.js servers can leak memory too, through growing caches, global arrays or unclosed connections. Monitor memory over time in production, use heap snapshots on staging and set alerts for unusual growth.

Measuring success

After fixes, repeat your test flow many times and confirm memory returns to a stable baseline. Add a simple long-running test to catch regressions.

Test on mobile devices

Mobile browsers have less memory, so leaks crash tabs sooner. Include phones in long-running tests.

How we help

Our web app development team profiles and fixes memory and performance issues in complex front ends. Our web development team builds lean, fast pages, and our app development service applies the same practices to hybrid and cross-platform apps.

Frequently asked questions

How do I know if my web app has a memory leak?

If memory keeps rising during normal use and doesn't drop after garbage collection, you likely have a leak.

Does JavaScript garbage collection prevent leaks?

It frees unreferenced memory, but can't free objects your code still references accidentally.

What's the best tool to find memory leaks?

Browser developer tools, especially heap snapshots in Chrome DevTools, are the most practical starting point.

This guide is general information, not legal, tax or financial advice, and details such as prices, features and rules can change. Product and company names mentioned are trademarks of their respective owners and are used only to identify those products; UK Technology Hub is not affiliated with or endorsed by them.

Related guides

How to fix React hydration errors

Why React hydration mismatches happen in server-rendered apps, how to read the error, and reliable fixes for dates, browser-only code, invalid HTML and extensions.

How to fix layout shift (CLS) on your website

What causes content to jump while pages load, how Cumulative Layout Shift is measured, and practical fixes for images, ads, fonts, banners and embeds.

How to fix the WordPress white screen of death

A calm, step-by-step guide to diagnosing and fixing a blank white WordPress page, from recovery mode and debug logs to plugins, themes and memory limits.

All bug fixes & troubleshooting guides