How useCallback Keeps Old Renders in Memory (and Why It Still Happens in 2026)
by Abdelkader Settah
November 26, 2026
How useCallback Keeps Old Renders in Memory
TL;DR: In V8 (Chrome, Edge, Node), all functions created in the same scope share one hidden “context” object. If one function uses a big object, every function from that scope keeps it alive. useCallback keeps functions from old renders, so it can keep old renders’ data alive too. Two memoized callbacks that reach each other can build a chain that never gets freed. I ran the test in Chrome 158 with React 19.3.1: 10 clicks kept 110 MB in memory. The fix is usually small: create big objects once, keep scopes small, and memoize less.
This post assumes you know what useCallback does. Kevin Schiener first described this in Sneaky React Memory Leaks (2024). I wanted to know if it still happens today, so I tested it.
The rule most of us never learned
A closure is a function that remembers the variables around it. Most developers picture it like this: each function keeps only the variables it uses.
That is not how V8 does it. When a scope runs, V8 creates one context object for the whole scope. Every closure created in that scope points to the same context. If any closure uses a variable, that variable goes into the shared context.
So a function can hold memory it never touches:
function makeHandlers() {
const big = new Uint8Array(50 * 1024 * 1024); // 50 MB
const usesBig = () => big.length; // only this one reads `big`
usesBig();
return () => 'I never touch big'; // ...but this one keeps it alive
}
const small = makeHandlers();
Output from Node 26.8.1, after forcing garbage collection:
small fn alive: 50 MB
small fn dropped: 0 MB
The returned function never reads big, but big lives in the shared context, so 50 MB stays in memory until small is gone. Jake Archibald showed the same thing happens in every major engine in Garbage collection and closures.
Why this matters in React
Every render of a function component is a new function call, so every render gets a new context. Normally that is fine. Old renders’ functions are thrown away and their contexts go with them.
useCallback changes that. It keeps the function from an old render until a dependency changes. That old function points to its old context, and the old context holds everything any closure from that render used.
Here is a component that looks harmless:
function Editor() {
const [countA, setCountA] = useState(0);
const [countB, setCountB] = useState(0);
const pixels = new Uint8Array(10 * 1024 * 1024); // 10 MB, new every render
const handleA = useCallback(() => setCountA(countA + 1), [countA]);
const handleB = useCallback(() => setCountB(countB + 1), [countB]);
const handleBoth = () => {
handleA();
handleB();
console.log(pixels.length);
};
return (
<>
<button onClick={handleA}>A</button>
<button onClick={handleB}>B</button>
<button onClick={handleBoth}>Both</button>
</>
);
}
handleBoth uses handleA, handleB and pixels, so all three go into each render’s context. Now click A, then B, then A again:
- Click A.
countAchanged, so React makes a newhandleA.countBdid not change, so React keeps the oldhandleBfrom render 0. The new render’s context holds that oldhandleB, which holds render 0’s context and render 0’spixels. - Click B. React makes a new
handleBand keepshandleAfrom render 1. Render 2’s context points to render 1, which points to render 0. - Click A again. The chain gets one link longer.
Every link has its own 10 MB pixels, and nothing can be freed because each context points to the one before it.
I tested it on today’s V8
I wanted numbers from a current engine, not a 2024 screenshot. I put the component above in a small Vite + React 19.3.1 app, opened it in Chrome 158, and clicked A and B in turn 10 times. A FinalizationRegistry counts how many of the 10 MB objects are still alive after garbage collection.
| Version | After 10 alternating clicks |
|---|---|
pixels created on every render | 110 MB (11 copies of 10 MB) |
pixels created once and kept in a ref | 10 MB |
Chrome 158.0.8127.88 driven by Puppeteer, React 19.3.1 production build, with garbage collection forced through the DevTools protocol before each count. The development build gave the same numbers, and so did React 19.3.1 running in Node v26.8.1. That is 1 copy from the first render plus 1 per click, the same result Kevin saw in 2024. Nothing has changed.
The test app and the measuring script are on GitHub: react-closure-leak-test. Run npm run measure to get the same numbers on your machine.
Does the React Compiler fix it?
No. The React Compiler memoizes functions for you, and memoizing is exactly what keeps old functions and their contexts alive. Kevin wrote a follow-up, How the React compiler won’t save you, showing the same leak with compiled code. If anything, more automatic memoization means more places where an old context can stay around.
More examples of the same leak
The shared context is not a React thing. Any function that lives longer than the scope it was created in can carry that scope’s data with it. Here are three more places it shows up, plus two small fixes. I ran the plain JavaScript examples in Node 26.8.1 and the React one in the same Chrome test app, with forced garbage collection each time. The numbers are from those runs.
Example 1: a timer that never reads the data
function startDashboard(errorCodes) {
const report = new Uint8Array(20 * 1024 * 1024); // 20 MB
const hasErrors = errorCodes.some((code) => report.includes(code));
console.log({ hasErrors });
// This callback never reads `report`...
return setInterval(() => fetch('/api/ping'), 30_000);
}
interval running: 20 MB
interval cleared: 0 MB
The some() callback reads report, so report goes into the function’s shared context. The interval callback lives in the same context, so the 20 MB report stays in memory for as long as the dashboard keeps pinging. That can be hours.
Fix A: move the heavy work into its own function. The big object then lives in a different scope, and that scope ends when the function returns:
function checkErrors(errorCodes) {
const report = new Uint8Array(20 * 1024 * 1024);
return errorCodes.some((code) => report.includes(code));
}
function startDashboard(errorCodes) {
console.log({ hasErrors: checkErrors(errorCodes) });
return setInterval(() => fetch('/api/ping'), 30_000);
}
interval running: 0 MB
Fix B: drop the reference when you are done. Declare it with let and set it to null. The context still exists, but it no longer points at the 20 MB:
function startDashboard(errorCodes) {
let report = new Uint8Array(20 * 1024 * 1024);
const hasErrors = errorCodes.some((code) => report.includes(code));
console.log({ hasErrors });
report = null; // free it before the long-lived callback is created
return setInterval(() => fetch('/api/ping'), 30_000);
}
interval running: 0 MB
I prefer Fix A. Fix B works, but the next person to edit the function can easily move the null line or remove it.
Example 2: a subscription without cleanup
You already know that forgetting to unsubscribe is a leak. What you might not know is how much it leaks:
function Chart({ rawData }) {
const series = buildSeries(rawData); // large array, ~5 MB
const getTooltip = (index: number) => series[index];
useEffect(() => {
const onThemeChange = () => repaint();
themeBus.on(onThemeChange);
// forgot: return () => themeBus.off(onThemeChange);
}, []);
return <Canvas series={series} getTooltip={getTooltip} />;
}
onThemeChange is tiny, and it never reads series. But it was created inside a render whose context holds series, because getTooltip uses it. So each listener left in themeBus keeps a whole render’s data alive, not just a small function.
I mounted and unmounted this 10 times in the Chrome test app:
10 mounts, no cleanup: 50 MB
10 mounts, with cleanup: 0 MB
The same applies to window.addEventListener, WebSocket handlers, ResizeObserver callbacks and store subscriptions. Always return the cleanup function.
Example 3: one small handler keeps everything
Context is shared per scope, not per function. So keeping one function costs the same as keeping all of them:
function buildRowHandlers(rows) {
const decoded = decodeCsv(rows); // ~15 MB
const firstEmpty = rows.findIndex((row, i) => decoded[i] === 0);
console.log({ firstEmpty });
return rows.map((row) => () => selectRow(row.id));
}
5 row handlers kept: 15 MB
1 row handler kept: 15 MB
0 row handlers kept: 0 MB
Each row handler only needs row.id, but every one of them points at the shared context, and that context holds decoded. Saving even one handler somewhere (in a cache, a store, a “last clicked” variable) keeps all 15 MB. Fix A works here too: decode and check the CSV in a helper, and only return the small handlers.
What the examples have in common
Look for this shape in your code:
- A function creates a big object.
- Some short-lived callback in the same function uses it (
some,filter,findIndex, a tooltip helper). - A long-lived callback is created in the same function (a timer, a listener, a memoized handler, something you return or store).
The long-lived callback keeps the big object alive even though it never reads it. Break any one of the three and the memory goes away.
Will you hit this?
Probably not in a normal form or list. Holding a few extra strings and numbers for a while costs nothing you will notice.
You should watch for it when all of these are true:
- the component lives a long time (an app shell, an editor, a dashboard that is never unmounted)
- it creates large objects during render (image or canvas data, video frames, big parsed JSON, large derived arrays)
- it has several memoized callbacks, and some of them call each other
Four fixes
1. Create big objects once, not every render. This is the fix that took the test from 110 MB to 10 MB. Use a lazy useState or a ref:
const [pixels] = useState(() => new Uint8Array(10 * 1024 * 1024));
// or
const pixelsRef = useRef<Uint8Array | null>(null);
if (!pixelsRef.current) pixelsRef.current = new Uint8Array(10 * 1024 * 1024);
If the data has to be released at some point, you own that now: set the ref to null when you are done with it.
2. Keep scopes small. A callback can only keep alive what is in its own scope. Move each callback into a small custom hook, and its context holds only that hook’s variables, not the whole component’s:
function useCounter() {
const [count, setCount] = useState(0);
const increment = useCallback(() => setCount((c) => c + 1), []);
return [count, increment] as const;
}
Using the updater form setCount((c) => c + 1) also removes count from the dependency list, so increment never changes and never chains to old renders.
3. Don’t let memoized callbacks call other memoized callbacks. The chain needs one function that reaches both handleA and handleB. Without it, the old contexts do not point to each other.
4. Memoize only where you measured a re-render problem. useCallback trades memory for fewer renders. Without a slow child wrapped in React.memo, you pay the memory and get nothing back.
How to check your own app
- Open Chrome DevTools, go to Memory, and take a heap snapshot.
- Use the feature that you suspect a few times.
- Take a second snapshot and pick Comparison.
- Sort by Size Delta. If you see big objects (
ArrayBuffer,Uint8Array, large arrays) whose count keeps going up, select one and read the Retainers panel. A chain ofcontextentries pointing at your callbacks is this bug.
Summary
Functions created in the same scope share one context in V8, so a function can keep alive data it never uses. useCallback keeps functions from old renders, and two memoized callbacks that reach each other can chain every old render together. In Chrome 158 with React 19.3.1, 10 clicks held 110 MB instead of 10 MB. The same thing happens with timers, subscriptions and lists of small handlers: a function that never reads a big object can still keep it alive. The React Compiler does not change this. Create large objects once, keep callbacks in small scopes, avoid memoized callbacks that call each other, and only memoize where it pays off.
Further reading
- My test project: react-closure-leak-test (Vite + React 19, measured in Chrome)
- Kevin Schiener, Sneaky React Memory Leaks: How useCallback and closures can bite you
- Kevin Schiener, Sneaky React Memory Leaks: How the React compiler won’t save you
- Jake Archibald, Garbage collection and closures
- Vyacheslav Egorov, Grokking V8 closures for fun (and profit?)