Description
Do you want to request a feature or report a bug?
Possible bug, or unexpected change in internal behavior
What is the current behavior?
We are seeing a substantial memory leak in our codebase in versions of React > 16.5.2. Bisecting the issue, it seemed to appear at commit 7bee9fb.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
Our apologies, we are unable to fully isolate the issue to a minimal demo, and suspect it is not a bug but a behavior change which our code relied on. The leak seems to be in the messages area, where many components are created and released quickly.
What is the expected behavior?
Old fibers should be garbage collected, but cannot be. Components are retained in memory.
We have run the affected code area in StrictMode, which allowed us to identify some event leakage but even when this was addressed (by moving listeners to componentDidMount from the constructor) the memory leak continues. There are some findDomNode usages remaining, but even if we move those it does not fill the leak.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.5.3+
We are seeking suggestions on what internal could be causing retention of components and nodes by our code, and better ways to isolate the issue.
Thank you!
-Mike
Activity
[-]Unable to solve for memory leak > on React 16.2.5[/-][+]Memory leak on React > 16.2.5[/+]Dergash commentedon Jan 31, 2019
We've encountered similiar issue on 16.6.3.



Allocation instrumentation on timeline shows that our application allocates a lot of FiberNode objects:
And timeline shows that they are never collected, heap grows up without GC firing when we expecting it:
At the same time, timeline on 16.8.0-alpha.1 (16.7.0 shows the same) GC seems to fire correctly:
May be it was already fixed by this?
0e9cb3f It's tagged 16.7.0
mgreer commentedon Jan 31, 2019
Sadly no, that had been our hope but leak persists.
proluk commentedon Jan 31, 2019
We had similar problem but we fixed that by upgrading react-dom and react lib to 16.7
gaearon commentedon Feb 8, 2019
The only relevant fix I'm aware of is #14276 which got into 16.7.0. We'd need a reproducing case to help more.
Remember to test production version, and also that we retain a reference to deleted children for one more update (#12420 (comment)).
ejallday commentedon Feb 22, 2019
@mgreer take a look at #14284 Any of your components have
<input type="password">
near the problem?Pitasi commentedon Apr 30, 2019
Updating from React 16.6 to 16.8 drastically improved our memory usage, but it's still there, slow and inexorable.
Currently I have installed react and react-dom 16.8.6.
More info: I'm using Electron, BlueprintJS and Redux, I have a polling component that updates itself making some http requests and dispatching a Redux action.
From devtools I can see a lot of FiberNode and they are never deleted, they just keep growing.
My naive implementation of the component it's something like this:
if I can help providing any information, just let me know. Thanks!
raRaRa commentedon May 8, 2019
I have very similar memory leak with video and audio elements, perhaps related in some way? Do you find a lot of detached nodes if you create a heap snapshot under memory and search for detached in the class filter? You can check out my issue here, which is very easy to reproduce with a simple demo: #15583
hamham91 commentedon May 14, 2019
We have a somewhat similar memory leak (although we're experiencing it on all React 16.x versions) where there are a lot of detached nodes that never get garbage collected. The issue goes away if we downgrade to React 15.x.
You can see the documentation in this issue (along with a reproducible example): #15637
magmaliang commentedon May 24, 2019
I've got the same problem. Any one solved this problem except demotion?
tlenex commentedon May 24, 2019
There is an open PR #15157.
qianlongdoit commentedon Jun 2, 2019
This case maybe helpful.

Potential memory leak when navigating between pages
3 remaining items
gaearon commentedon Jul 26, 2019
#16115 was recently merged and will be out in 16.9.0. It might improve things.
josh-audio commentedon Aug 6, 2019
Upgrading to
16.9.0
completely fixes the problem for me. 🎉abirmingham commentedon Aug 9, 2019
Oddly, upgrading to
16.9.0-rc.0
decreases my memory leak by an order of magnitude (25MB -> 3MB per page transition), but only whenNODE_ENV=development
. When I useNODE=ENV=production
, the leak reappears.gaearon commentedon Aug 10, 2019
What minifier are you using? We’ve seen perf issues caused by a buggy minifier version.
abirmingham commentedon Aug 13, 2019
Very interesting, @gaearon, thanks for the tip. I'm using
uglifyjs-webpack-plugin@1.2.4
anduglify-js@3.4.10
. Disabling minification viamodule.exports.optimization.minimize: false
inwebpack.config.js
cuts the leak from 10MB per tree remount to 5MB. I will continue investigation.EDIT: I was able to resolve a couple memory leaks in my own application. Unfortunately this didn't move the needle. However, applying #15157 cut the leak from 5MB to <1MB. Hopefully that MR will go in soon :)
EDIT: Issue is ongoing. Here are a collection of heap snapshots with 10 tree remounts in between each snapshot. This is without #15157 applied. It appears that react is the culprit, but perhaps devtools is misleading here, and user-land memory leaks will appear to originate in react? Any advice greatly appreciated! :)

golden-mountain commentedon Dec 23, 2019
@gaearon and all,
I can easily reproduce it using react-dnd, here are my steps when I was working on following nested IDE projects, from below image we can see a lot of framed boxes, these boxes can be dragged and dropped.
if I use both drag and drop, will raise memory leak
my code:
setup drop
`
const [{ isOverCurrent }, drop] = useDrop({
accept: [DND_IDE_NODE_TYPE, DND_IDE_SCHEMA_TYPE, DND_IDE_DATASOURCE_TYPE],
canDrop: (item: DragItem, monitor: DropTargetMonitor) => {
if (!refDrop.current) {
return false;
}
const draggingNode = item.uinode;
const hoverNode = uinode;
},
hover: async (item: DragItem, monitor: DropTargetMonitor) => {
if (!monitor.canDrop()) return;
},
drop: async (item: DragItem, monitor: DropTargetMonitor) => {
if (!monitor.canDrop()) return;
const draggingNode = item.uinode;
const targetNode = uinode;
},
collect: monitor => ({
isOverCurrent: monitor.isOver({ shallow: true })
})
});
`
setup drag using useDrag
const [, drag] = useDrag({ item: { type: DND_IDE_NODE_TYPE, uinode } });
use drag and drop at same element, found the memory increasing much

drag(drop(refDrop));
use drop only, no memory leak
use drag only, memory increasing much
stale commentedon Apr 12, 2020
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
stale commentedon Apr 19, 2020
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!