-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Fix Memory Leak in AnimatedWithChildren #49217
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
Conversation
Hi @c-miles! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
__removeChild(child: AnimatedNode): void { | ||
const index = this._children.indexOf(child); | ||
if (index === -1) { | ||
console.warn("Trying to remove a child that doesn't exist"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assumed duplicate removals are expected during cleanup; with __addChild
now preventing duplicates, the warning in __removeChild
causes unnecessary test failures and log noise.
__addChild(child: AnimatedNode): void { | ||
// Prevent adding duplicate animated nodes. | ||
if (this._children.includes(child)) { | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check prevents duplicate animated nodes from being added, which fixes the memory leak we observed.
The fact that _addChild
is being called repeatedly with the same node may suggest an opportunity for further architectural review, though this fix addresses the immediate issue.
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
I’m following up on this pull request. Could someone please review it and let me know if any further modifications are needed to move it forward? Thanks in advance. |
Approved |
I would expect that if Could you find a test for this which doesn't rely on calling |
@javache Your expectation about All classes extending AnimatedWithChildren (AnimatedValue, AnimatedTransform, AnimatedInterpolation, AnimatedStyle, etc.) properly call The issue occurs when multiple __removeChild(child: AnimatedNode): void {
const index = this._children.indexOf(child);
if (index === -1) {
// Remove fails if child not found
return;
}
this._children.splice(index, 1);
} I chose to prevent duplication at the I've added a test demonstrating the 1:1 relationship without explicitly calling An alternative solution would be using a Set instead of an Array for |
Thanks for the detailed pull request, @c-miles. If I understand @javache's point, it should always be the case that if Do I understand correctly that the problem you're raising here is the unbounded growth of If so, then I understand why you are approaching the solution in this way. I think @javache's concern with this fix is the expectation that An alternate approach that prevents unbounded growth of
Side effects (e.g. |
Anything preventing this from being merged?? This is a critical issue in a few of my apps, would love to see this fix merged. |
@yungsters Thank you for the detailed feedback. Your Map based reference counting suggestion makes complete architectural sense. After digging deeper, I found the root cause is actually in React Native Web, not core. React Native Web's Removing that constructor call in React Native Web completely fixes the leak. I'll open a pull request there to address it at the source. @micahlt Sorry for the delay! We forked RN with this fix and I moved roles and forgot about the PR. If you're using React Native Web, that's where the fix needs to be. If you're on iOS/Android core, I'm curious about your setup as I couldn't reproduce it there. Closing this since the issue doesn't affect core. Thanks for pushing me to investigate further! |
Here's the pr on web: Fix memory leak in AnimatedProps constructor |
|
Summary:
This pull request fixes an issue in
AnimatedWithChildren
where duplicate children were being added during repeated re-renders.In our long-lived app, which uses many animations, this caused the children array to grow unchecked, eventually degrading performance and freezing the app.
Here's a reproduction that visually demonstrates performance degradation over 20–30 seconds.
This change prevents adding the same child more than once, eliminating the memory leak and restoring smooth performance.
Changelog:
[General] [FIXED] - Prevent duplicate child additions in
AnimatedWithChildren
to prevent memory leak.Test Plan:
Added a new test file (AnimatedWithChildren-test.js) that verifies:
Manual Verification:
Below are before-and-after screenshots from my test app’s memory dev tools. You can see that within one minute, the test app running on main grew by 95 MB, compared to the test app running with the fix that prevents duplicates. Note that the dips on the right side of the image are caused by taking heap snapshots.
closes #48860