-
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
Fix to the memory leak issue with useEffect #14153
Conversation
Details of bundled changes.Comparing: 051272f...d05da56 react-dom
react-art
react-test-renderer
react-reconciler
react-native-renderer
scheduler
Generated by 🚫 dangerJS |
Revert changes to unmount of fiber Fix Flow errors Fix prettier Add a __key field to create functions to check identity revert change
a4f0560
to
bb37b80
Compare
|
@sophiebits In order to remove the memory leak on unmount, you need to Furthermore, when I tried the approach of always rerunning some hook tests fail, specifically |
The big question is if we want to be optimizing for the common case:
In this case this will always be a new function so all attempts to avoid recalling it will just slow it down. Since it will always reexecute we ideally don’t need to even create a hook object for it. But we don’t know before we call it if it will also have a destructor associated with it. Unless we add those to a different list somehow. |
So this doesn’t work when you have a destroy function right?
Still would leak since we need to store the callback somewhere. Similarly what happens with I’m not sure what the bar is for when we’re ok with leaking vs not and which scenarios. |
@sebmarkbage I'm not super sure about all cases. The underlying issue is here is retention of closure references and their contexts. |
@sebmarkbage I tried the alternative approach as I outlined above: #14218 |
This is a possible solution to the memory leak issues @localvoid alerted us to in this repro https://codesandbox.io/s/lz61v39r7.
I'm not 100% sure if the changes in this PR break something else, so it would be good to get @acdlite eye's on this. Specifically this PR makes the following changes:
create
function, wenull
out thecreate
field after. If we don't do this, the context of thecreate
function closure causes memory retainment.create
function fromuseEffect
to the dependencies array. Doing so, brings the memory retainment issue in the above point. Instead, we tag thecreate
function with a field__key
and assign a uniquesymbol
to the function. We then use this symbol as the key in the dependencies.I can confirm that in the cases of the repro the memory leak issues no longer exist with these two changes. Unfortunately, I don't know of any good way to make tests that can validate memory leaks. If anyone has ideas on how we might test these changes, please let me know.