-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
componentDidMount inside a class causing a memory leak #14630
Comments
React currently retains the previous version of an unmounted child tree until the next update. If you flip back and forth between showing and hiding the same tree, that doesn't give it a chance to be cleaned up. See #12420 (comment). |
Thank you for your answer. If i understand well, it's an optimisation in case of showing/hiding elements. What happens if the child is never flipped back? For instance, you can have two tabs A and B. Thus if I want to force React to release the heavy tree B in memory, I just need to update multiple times the tab with light content for instance? How do you manage it at facebook? EDIT:
|
We've just had a major leak related to |
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! |
Hi,
I have a very weird memory leak that seems to be related to componentDidMount declaration. Memory is not freed after unmounting component.
Used code
Version: react 16.7
Mode: developper or production (same behaviour)
Here is the code I use to hide or display a list of items
Steps to reproduce
CASE 1
Use google chrome and display the performance monitor to study JS Heap size and Dom Nodes.
When you look at performance monitor, you can see that around 8000 nodes are still in memory (and JS Heap is higher than before mounting components as well).
If you redo 1) and 2) multiple times, you will see nodes going to 16000 then going back to 8000, ... etc.
Thus, memory is freed after the first unmount operation, but the first one is not. The weird thing is that if you do 1), 2), 2), 2), then it is freed.
CASE 2
Use the same code but remove the "componentDidMount" function in the class.
Do 1) and 2), then after few secondes memory is freed automatically (nodes and js heap) => expected behaviour
Behaviour expected
I was expected that the memory would be freed after unmounting a component, like in the 2nd case. That's a real issue when you mount.unmount big list multiple times, then js heap is going very high.
The text was updated successfully, but these errors were encountered: