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 Animated API by managing parent-child references #48993

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

Conversation

rodriguescarson
Copy link

Summary:
This pull request addresses a memory leak issue in React Native's Animated API by ensuring proper cleanup of parent-child relationships between AnimatedNode instances. The existing implementation did not manage bidirectional references effectively, leading to potential memory retention and performance degradation.

The fix introduces public methods (addChild and removeChild) to provide a consistent API for managing child nodes and modifies the internal methods to maintain a bidirectional relationship between parent and child nodes. This ensures proper cleanup of references, allowing garbage collection to function as intended.

Changelog:
[GENERAL][FIXED] - Resolved memory leak in Animated API by managing parent-child references between AnimatedNode instances.

Test Plan:
Verified that child nodes are correctly added and removed using the new public methods addChild and removeChild.
Confirmed that parent nodes are properly added to and removed from the _parents array of child nodes.
Ran the existing Animated API tests to ensure no regressions in functionality.
Monitored memory usage in Chrome DevTools to verify that parent-child relationships are correctly cleared, preventing memory leaks.
Tested the changes in a sample React Native project with complex animations to ensure stability and performance.
This description provides a clear motivation, solution summary, and test validation steps for reviewers and maintainers.

@facebook-github-bot
Copy link
Contributor

Hi @rodriguescarson!

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!

@rodriguescarson
Copy link
Author

Yes I have done this

@facebook-github-bot facebook-github-bot added 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. labels Jan 28, 2025
@javache javache requested a review from yungsters January 28, 2025 11:59
@@ -56,6 +49,7 @@ export default class AnimatedWithChildren extends AnimatedNode {
console.warn("Trying to remove a child that doesn't exist");
return;
}
child.removeParent(this); // Remove parent reference from the child
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yarn build was failing because child doesn't always implement addParent or removeParent.

I had to add checks (typeof child.addParent === 'function' and typeof child.removeParent === 'function') to avoid calling nonexistent methods.

I’m not necessarily recommending to adopt this; I just wanted to share what I had to do locally to test the rest of these changes.

(See attached screenshot of the terminal output.)

yarn build error

Comment on lines 38 to +39
this._children.push(child);
child.addParent(this); // Maintain bidirectional parent-child relationship
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m still seeing a memory leak as we keep adding the same child to _children multiple times. Here's an example where there's 334 children in the array while only 2 is expected.

Screenshot 2025-01-28 at 8 08 38 PM

I resolved this locally by adding an !this._children.includes(child) check just like you're doing in addParent below

Here's my original issue on react-native-web for reference, where I implemented just that change to resolve it: RN-web issue

I was able to achieve the same result by tweaking the changes to your updated __addChild method, which resolves the issue

  __addChild(child: AnimatedNode): void {
    if (!this._children.includes(child)) {
      if (this._children.length === 0) {
        this.__attach()
      }
  
      this._children.push(child)
  
      if (typeof child.addParent === 'function') {
        child.addParent(this)
      }
  
      if (this.__isNative) {
        child.__makeNative(this.__getPlatformConfig())
        connectAnimatedNodes(this.__getNativeTag(), child.__getNativeTag())
      }
    }
  }

@rodriguescarson
Copy link
Author

Hey, got busy, can someone contribute to this pr and close. I think the reveiwers have done a fantasic job, can take some help from there. @c-miles @mojodna @jsierles

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.

3 participants