Skip to content
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 Memory Leak in AnimatedWithChildren #49217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

c-miles
Copy link

@c-miles c-miles commented Feb 5, 2025

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:

  • A child is added only once when __addChild is called multiple times.
  • The children array is correctly updated when children are removed.

Manual Verification:

  • I configured a test app to use my custom React Native build from my fork and confirmed that the memory leak was eliminated by monitoring the children array during repeated animations and updates with memory dev tools.

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.

Main
This branch

fixes #48860

@facebook-github-bot
Copy link
Contributor

Hi @c-miles!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@@ -53,7 +57,6 @@ export default class AnimatedWithChildren extends AnimatedNode {
__removeChild(child: AnimatedNode): void {
const index = this._children.indexOf(child);
if (index === -1) {
console.warn("Trying to remove a child that doesn't exist");
Copy link
Author

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.

Comment on lines 41 to +45
__addChild(child: AnimatedNode): void {
// Prevent adding duplicate animated nodes.
if (this._children.includes(child)) {
return;
}
Copy link
Author

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 effectively addresses the immediate issue.

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 6, 2025
@c-miles c-miles marked this pull request as ready for review February 6, 2025 16:43
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory Leak in Animated API
2 participants