Most memory leaks in React apps come from one place: an effect that starts something and never stops it. A resize listener, a timer or a WebSocket keeps running after the component is gone, and it holds on to that component's memory while it does.
We measured it on React 19.2. Mounting and unmounting a component 50 times left 50 resize listeners, 50 running timers and 50 open sockets behind. A three-line cleanup function brought every one of those numbers to zero.
It hurts most in apps people leave open for hours, like dashboards, admin panels and chat. The tab gets slower, then heavier, and eventually the browser gives up on it.
The takeaway? If your effect starts something, it should return a function that stops it.
Key Takeaways
- Almost every React memory leak is a missing
useEffectcleanup: a listener, timer, socket or subscription that outlives its component.- You find leaks with Chrome DevTools heap snapshots. Take one, repeat the action a few times, take another and compare.
- React 18 removed the old "state update on an unmounted component" warning, so a quiet console doesn't mean you're leak-free.
What is a memory leak in React?
A memory leak happens when your app keeps a reference to something it no longer needs, so the garbage collector can't free it. In React, the usual victim is a component that has unmounted but is still referenced from somewhere else.
Here's how it plays out. Your effect adds a listener to window. That listener calls setState, so it's tied to the component. The component unmounts, but window still holds the listener, so nothing it points to can be freed. Repeat that on every route change and it adds up.

How to fix memory leaks in React: clean up every effect
React runs your cleanup function when the component unmounts, and before the effect runs again with new dependencies. That's your chance to undo whatever the effect set up. The useEffect reference covers the exact timing.
Here are the four leaks we see most often, and the fix for each.
1. Event listeners
This is the classic one. It looks harmless:
// Leaks: the listener is never removed
function WindowWidth() {
const [width, setWidth] = useState(0);
useEffect(() => {
window.addEventListener("resize", () => setWidth(window.innerWidth));
}, []);
return <p>Width: {width}px</p>;
}Every time this component mounts, another listener piles onto window. Here's the fixed version:
function WindowWidth() {
const [width, setWidth] = useState(0);
useEffect(() => {
function handleResize() {
setWidth(window.innerWidth);
}
handleResize();
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}, []);
return <p>Width: {width}px</p>;
}Notice the named function. removeEventListener only removes the exact function you added, so an inline arrow can never be taken off again. Reading the width inside the effect also keeps this safe with server rendering in Next.js.
2. Timers: setInterval and setTimeout
An interval doesn't know the component is gone. It keeps firing, keeps calling setState and keeps its closure alive for as long as the tab is open.
function Clock() {
const [now, setNow] = useState(() => new Date());
useEffect(() => {
const id = setInterval(() => setNow(new Date()), 1000);
return () => clearInterval(id);
}, []);
return <time>{now.toLocaleTimeString()}</time>;
}The same goes for a long setTimeout: keep the id and call clearTimeout in cleanup. If you've ever wondered why a callback fires after everything else has moved on, our explainer on how the JavaScript event loop handles asynchronous code walks through it.
3. Fetch requests
This one comes with a caveat. A request that finishes after unmount usually isn't a true leak. The promise settles and gets garbage-collected, which is why the React team called the old warning misleading.
It's still worth cancelling. A late response wastes bandwidth, and when a prop like userId changes quickly, an old response can land after a new one and show the wrong user.
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
useEffect(() => {
const controller = new AbortController();
fetch(`/api/users/${userId}`, { signal: controller.signal })
.then((res) => res.json())
.then(setUser)
.catch((err) => {
if (err.name !== "AbortError") console.error(err);
});
return () => controller.abort();
}, [userId]);
return <h2>{user ? user.name : "Loading…"}</h2>;
}An AbortController cancels the request on unmount and whenever userId changes. The AbortError check matters because cancelling rejects the promise on purpose. Skip the old isMounted ref trick; it hides the symptom and cancels nothing.
4. WebSockets and subscriptions
Anything you subscribe to needs an unsubscribe: sockets, event emitters, Firebase listeners, store subscriptions. Forget it and the connection stays open with your component attached.
function LivePrice({ symbol }) {
const [price, setPrice] = useState(null);
useEffect(() => {
const socket = new WebSocket(`wss://example.com/prices/${symbol}`);
socket.onmessage = (event) => setPrice(JSON.parse(event.data).price);
return () => socket.close();
}, [symbol]);
return <span>{price ?? "…"}</span>;
}If you're reading from an external store, useSyncExternalStore makes the pairing hard to forget: its subscribe function has to return the unsubscribe.
Watch for these too
Charts, maps, rich text editors and video players often build their own DOM and attach their own listeners. Create the instance inside an effect and call the library's destroy, dispose or remove method in the cleanup.
Also watch module-level variables. An array or Map declared at the top of a file lives as long as the tab does. Anything a component pushes into it stays there until you take it out.
How to find memory leaks in a React app
Chrome's Memory panel is the most reliable way to confirm a leak. The trick is to compare two heap snapshots, not to stare at one. Here's the routine:
- Open the page in a window without extensions, open DevTools and go to the Memory tab.
- Click the trash-can icon to force garbage collection, then take a heap snapshot.
- Repeat the action you suspect five to ten times: open and close the modal, or navigate away and back.
- Force garbage collection again and take a second snapshot.
- Switch the second snapshot to the Comparison view. Look for your component names, listeners or "Detached" elements whose count grows with every repeat.
- Click a suspicious entry and read the Retainers panel. It tells you exactly what's still holding on to it.

Detached elements deserve a special look. Chrome's guide to fixing memory problems calls detached DOM nodes a common cause of leaks. They're elements removed from the page that some JavaScript still references.
Turn on Strict Mode as well. In development, React runs one extra setup-and-cleanup cycle for every effect, so a missing cleanup shows up early, as two connections or two listeners. The Strict Mode docs explain the double run.
How to prevent memory leaks in React
- Treat every effect that listens, polls, subscribes or fetches as a pair: setup and cleanup, written together.
- Keep Strict Mode on in development, and don't remove it to make a double effect go away.
- Write fewer effects. Plenty of them aren't needed at all, as we covered in you might not need useEffect anymore.
- Run the snapshot check on your longest-lived screens before a big release, not after users complain.
If you're on Next.js, leaks are only half the story. Our guide to Next.js performance optimization covers shipping less JavaScript in the first place.
When rising memory isn't a leak
Not every climbing memory graph is a bug. Caches, decoded images and lazy-loaded code are meant to stay in memory for a while. The garbage collector also runs on its own schedule, not when you happen to be watching.
So force garbage collection before you judge. If memory drops back to roughly where it started, you're fine. If it keeps stepping up with every repeat of the same action, that's a leak.
Cleanup functions won't fix everything either. A global store or cache that keeps data forever by design needs a size limit or an expiry, not a return in an effect.
How we checked this
We ran every code sample here in a production build of a Next.js 16.1 app with React 19.2.3, in Chrome. A test page mounted and unmounted each component 50 times and counted what was left behind. Sockets and fetches used stand-ins that count open connections and requests instead of hitting a real server.
ExampleWithout cleanupWith cleanup
Resize listeners left on window
50
0
Intervals still running
50
0
WebSockets still open
50
0
Requests cancelled on unmount
Not tested
50 of 50
The React 18 change, the Strict Mode behaviour and the DevTools workflow come from React's and Chrome's own documentation, linked above.
Frequently Asked Questions
How do I know if my React app has a memory leak?
Watch memory in the DevTools Memory panel while you repeat one action, like opening a modal. If it climbs with every repeat and doesn't come back down after you force garbage collection, you have a leak. Compare two heap snapshots to see exactly what's piling up.
Can useEffect cause memory leaks?
Yes, and it's the most common source. An effect that adds a listener, starts a timer or opens a subscription without returning a cleanup function leaves that work running after unmount. Returning a function that undoes the setup fixes it.
Does React still warn about memory leaks?
No. React 18 removed the "Can't perform a React state update on an unmounted component" warning, because it mostly fired for pending promises that weren't real leaks. You now have to find leaks yourself with DevTools.
Why does my useEffect run twice in development?
That's Strict Mode. React runs one extra setup-and-cleanup cycle in development to expose effects that don't clean up after themselves. It doesn't happen in production, and if your cleanup is right, you won't notice it.
Is calling setState after unmount a memory leak?
Not on its own. React ignores the update, and a finished promise gets garbage-collected. The leak is whatever keeps calling it, such as a listener, timer or socket nobody removed.
Start with your longest-lived screen
Pick the screen your users keep open the longest, run the two-snapshot check on it this week, and fix whatever keeps growing. That's usually one missing cleanup, and it's usually a three-line fix.
We build and maintain React apps for clients at Smartters. If you'd like help with a leak that won't go away, our React development team is happy to take a look. Get in touch.
