Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Memory leak slowing long sessions and SPAs: how to find and fix it

PRF-047Low impactOther toolBy FunnelGuardPublished Updated
On this page

What this issue is

Browsers free memory automatically when nothing refers to it any more (this is called garbage collection). A memory leak is when your code accidentally keeps a reference to something, so the browser cannot free it. Each time the user repeats an action, a little more memory piles up.

It matters most on single page apps (SPAs), where the page never fully reloads between views. Multi-step checkouts, configurators, dashboards and chat widgets stay open for a long time, so small leaks add up.

Common causes:

Usual sources of leaks
  • Event listeners added on every view or render and never removed (addEventListener without removeEventListener)
  • setInterval or setTimeout loops that keep running after the view is gone
  • DOM elements removed from the page but still referenced in a variable or array (detached DOM nodes)
  • Components in React, Vue or Angular that subscribe to a store, socket or observer and never unsubscribe
  • Global variables or caches that only ever grow
  • Third-party scripts or widgets that re-initialise on every route change

Why it costs you leads or sales

As memory grows, the browser spends more time on garbage collection and the page gets less responsive. Clicks lag, typing stutters and animations drop frames. On phones and older laptops the tab can be reloaded or crash, and the visitor loses their form progress or cart.

The business impact is usually low, because most visitors leave long before a leak matters. It hurts most on long, multi-step journeys, on app-like sites and on low-memory mobile devices, where it shows up as slow interactions late in a session.

Memory use during one long sessionExample numbers
Memory use during one long session (example)Start: 60 MB; After 10 views: 110 MB; After 30 views: 210 MB; After 60 views: 380 MB; After 100 views: 620 MB023847560110210380620StartAfter 10 viewsAfter 30 viewsAfter 60 viewsAfter 100 viewsMB

Example: a leaking single page app grows with every view the visitor opens, while a healthy one levels off. Numbers are illustrative.

A leak builds up until the tab struggles

Each view leaves a bit of memory behind until the page lags or crashes.

How to check if your site has it

You need a browser profiler. Use a desktop Chrome window with extensions turned off (an Incognito window works).

Test for a leak in Chrome DevTools
  1. Watch memory over time

    Open DevTools (F12), go to the Performance tab and tick Memory. Record while you repeat one action 20 to 30 times, such as opening and closing a modal or moving between SPA routes.

  2. Look at the JS heap line

    A leak shows as a staircase that keeps climbing. Healthy memory rises and drops back after garbage collection.

  3. Force a cleanup

    Click the trash can icon (Collect garbage) in the Memory or Performance tab. If memory stays high afterwards, something is still holding it.

  4. Take heap snapshots

    In the Memory tab choose Heap snapshot. Take one, repeat the action 10 times, collect garbage and take a second. Switch the second snapshot to Comparison view and sort by Delta to see which objects keep growing.

  5. Search for detached elements

    In a snapshot, type Detached in the class filter. Detached DOM nodes that remain after you leave a view point to leaked references.

  6. Check listeners

    In the Performance Monitor panel (More tools menu), watch JS event listeners and DOM Nodes. If either count climbs with each repeat of an action, you have found the leak.

How to fix it

The fix is always the same idea: whatever you set up when a view appears, tear down when it goes away. The exact place depends on your framework or platform.

Listener cleanup

Leaking

  • A new scroll listener is added every time the view renders
  • Old listeners keep references to removed elements
  • Listener count and DOM nodes climb with each view

Cleaned up

  • Listener is removed when the view unmounts
  • Timers are cleared and subscriptions cancelled
  • Listener count and DOM nodes stay flat
Fix the leak by platform
  1. Remove listeners you add

    Keep a reference to the handler and call removeEventListener, or pass an AbortController signal so one call removes many listeners.

  2. Clear timers

    Store the id from setInterval or setTimeout and call clearInterval or clearTimeout when the view closes.

  3. Drop references to removed elements

    Set variables to null or empty arrays and maps that hold removed DOM nodes. Use WeakMap or WeakRef for caches keyed on elements.

  4. Disconnect observers

    Call disconnect() on IntersectionObserver, MutationObserver and ResizeObserver, and close WebSocket connections when finished.

Clean up listeners and timers when a view closesjavascript
const controller = new AbortController();

window.addEventListener('resize', onResize, { signal: controller.signal });
window.addEventListener('scroll', onScroll, { signal: controller.signal });
const timerId = setInterval(refreshPrices, 5000);

function destroyView() {
  controller.abort();      // removes both listeners
  clearInterval(timerId);  // stops the timer
}
React: return a cleanup function from useEffectjavascript
useEffect(() => {
  const onResize = () => setWidth(window.innerWidth);
  window.addEventListener('resize', onResize);
  const id = setInterval(refreshPrices, 5000);

  return () => {
    window.removeEventListener('resize', onResize);
    clearInterval(id);
  };
}, []);

How to confirm it's fixed

Re-run the same test
  1. Repeat the exact action

    Do the same 20 to 30 repeats while recording the Performance tab with Memory ticked.

  2. Check the memory line

    The JS heap should rise and fall, then settle at a steady level rather than climbing.

  3. Compare snapshots again

    Delta counts for the previously growing objects should be near zero, and Detached results should be gone.

  4. Check counts in Performance Monitor

    JS event listeners and DOM Nodes should stay flat across repeats.

How to prevent it happening again

Prevention habits
  • Pair every setup (listener, timer, subscription, observer) with a teardown in the same code review
  • Add a memory-growth check to your release routine for SPAs and long flows: repeat key actions 30 times and watch the heap
  • Re-test after adding a new plugin, app, widget or tag manager tag
  • Use a linter rule for hook dependencies and cleanup in React projects
  • Track real user performance, including long-session behaviour, with a real user monitoring tool
  • Keep third-party scripts to those you need and update them regularly

How FunnelGuard handles this

This issue is outside what FunnelGuard checks. FunnelGuard runs your customer journeys on a schedule and checks that steps work, tracking hits fire and test leads arrive. It does not profile browser memory over a long session.

The kind of tool that fits is a browser profiler such as Chrome DevTools for diagnosing the leak, plus a real user monitoring (RUM) tool for seeing how slow sessions are for actual visitors. Use FunnelGuard alongside them to confirm your forms, checkouts and tracking still work after you ship a fix. There is a free plan with 1 site and 2 journeys, and a free read-only website audit.

Questions people ask

Why does my website get slower the longer it is open?

Most often the page is leaking memory: event listeners, timers or removed elements are never released, so memory use keeps growing. Record the Performance tab in Chrome DevTools with Memory ticked while you repeat an action and see if the heap climbs.

How do I find a memory leak in JavaScript?

Use Chrome DevTools. Take a heap snapshot, repeat an action about 10 times, collect garbage, take a second snapshot and use Comparison view sorted by Delta. Objects that keep growing, or Detached DOM nodes, point to the leak.

What causes memory leaks in single page apps?

The usual causes are listeners and timers that are not removed when a view changes, subscriptions that are never cancelled, references to removed DOM elements and ever-growing caches or global variables. Third-party widgets that re-initialise on each route change can also do it.

How do I fix a memory leak in React?

Return a cleanup function from useEffect that removes event listeners, clears timers and cancels subscriptions. Then re-profile with heap snapshots to confirm memory levels off.

What is a detached DOM node?

It is an element removed from the page that your code still refers to, so the browser cannot free it. Search for Detached in a Chrome heap snapshot to find them.

Is a memory leak bad for conversions?

It can be, mainly on long journeys, app-like sites and low-memory phones, where the page becomes laggy or crashes late in a session. Most visitors leave before it matters, so the impact is usually low.

Can FunnelGuard detect a memory leak?

No. FunnelGuard checks that journeys, tracking and lead delivery work, not browser memory over time. Use a browser profiler like Chrome DevTools and a real user monitoring tool for this.

Does growing memory always mean a leak?

No. Memory that rises then levels off is normally caching. A leak keeps climbing each time you repeat the same action, even after forcing garbage collection.

Does FunnelGuard catch this?

Other tool

Needs a different kind of tool.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.