-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak - React DOM keeps references to previous states/props/children of component #16138
Comments
Memory leak occurs when, you change the state of an unmounted component, so to remove this kind of warning, you better check is your component is mounted or not before updating its state.
componentWillMount() componentWillUnmount()
|
As you can see in the example I'm not unmounting |
In the updated example I console logged the sub-properties of rood div element to show that the redundant data and components still have references. |
Can you verify whether 16.9 solves this? |
I updated the example link. The external resources now point to react/react-dom 16.9.0 versions. |
@gaearon I upgraded both |
I am facing the same problem. I have a list(for example: list = [10, 20, 30]), use list.map return JSX.Element[], React & React-Dom: 16.9.0 |
Is there any update on this issue? We are seeing this as a significant issue when using Redux and container components. When our container componet(that is connected to redux store) passes props to the child components, and then the redux store updates. The child component props are being stranded in the dom with old reference(seen in the heap after forcing a garbage collection cycle). This is causing huge amounts of memory bloat when on a page that is connected to signalR for real time collaboration between users(as each redux update creates hundreds of stranded object references in the child components). I have verified that this is the cause by instead having all of the previously "dumb" pure child components be converted to Connected components and pull their props from redux instead of having the container component pattern control all of the store connections. this then correctly all references the single redux store object and garbage collection works as expected without stranded references. |
Any news on this issue ? |
Any update on this? |
I have an app with lots of components that get unmounted all the time and this is a big issue. Is there any viable workaround? I tried to cleanup the state field, but the memory is hold by other fields. |
I'm seeing this issue also, I noticed because I was working with large CSV/DB files with a Web Worker. I tried a few different tests and noticed my case was linked to a Detached system/Context from a resize observer that was using useEffect/useState. It was keeping a reference to the state of a nearby component somehow. It was an Animation test component that I could test for 60fps while parsing a 75MB file using a Web Worker. // No props, state passed to children
// The local state was in Converter
<PureComponentParent>
<AnimationFunctionComponent_w_useRef_useEffect_useState />
<ConverterComponent />
</PureComponentParent> I came across this when doing the following:
The previous state (large results) will still be retained after unmount, and can be seen in the heap snapshot from Chrome Dev Tools. If I hard refresh the browser, the memory will clear as expected |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution. |
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! |
Great that stalebot can close this if the developers simply ignore it long enough. Kudos, React Team! |
New issue opened here: #18790 Which has the main content of the original post, plus the description I added in the comments of this issue |
Do you want to request a feature or report a bug?
Report a bug.
What is the current behavior?
ReactDOM keeps references to previous states/props/children when component gets updated. All in all consuming three times as much memory as it really needed.
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:
Link to the example below (using production versions of
react
andreact-dom
):https://codesandbox.io/s/epic-bartik-pvgqx.
Consider following example:
I expect only one
MyBigObject
and oneMyItem
component to be in the memory. But instead I can see three of each in memory heap snapshot.UPDATE
As shown in the updated example the references to these objects and components can be accessed in the sub-properties of the root DOM element.
What is the expected behavior?
There's no justifiable reason to keep in memory unmounted components and previous states/props of component after it was updated.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.9.0, ReactDOM 16.9.0 (Production versions)
Mac/Win
The text was updated successfully, but these errors were encountered: