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 LogBoxModule #45261

Closed

Conversation

fannnzhang
Copy link
Contributor

@fannnzhang fannnzhang commented Jul 3, 2024

Summary:

To fix The Memory Leak Issue This change modifies the timing of view creation in the LogModule. The motivation behind this update is to address a potential memory leak issue. Previously, views were being created and held onto, which could lead to references to the Activity being retained even when they were no longer needed. By creating the view only when the show method is called and ensuring it is removed in the hide method, we can prevent these memory leaks and improve the overall memory management and stability of the LogModule.

Fixes #45080

  • Adjusted the timing of view creation to occur when the show method is called.
  • Ensured that the created view can be removed in the hide method.
  • This update addresses potential memory leaks by preventing the view from holding a reference to the Activity.

These changes improve memory management and stability within the LogModule.

Modify the timing of view creation in LogModule. The view is now created when the show method is called, and it can be removed in the hide method. This change resolves potential memory leaks caused by the view holding a reference to the Activity.

Changelog:

[ANDROID] [FIXED] - Fix LogModule to create view when show is called

@facebook-github-bot
Copy link
Contributor

Hi @fannnnzhang!

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!

Copy link

github-actions bot commented Jul 3, 2024

Warnings
⚠️ 📋 Missing Test Plan - Can you add a Test Plan? To do so, add a "## Test Plan" section to your PR description. A Test Plan lets us know how these changes were tested.

Generated by 🚫 dangerJS against 7519b01

@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 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 Jul 3, 2024
@dmytrorykun
Copy link
Contributor

@fannnzhang thank you for the PR! I've red the summary, and the attached issue, but I still need some clarification.
What exactly is leaking in the existing implementation?

@fannnzhang
Copy link
Contributor Author

fannnzhang commented Jul 4, 2024

@fannnzhang thank you for the PR! I've red the summary, and the attached issue, but I still need some clarification. What exactly is leaking in the existing implementation?

Thank you for your feedback! Let me clarify the existing implementation and the memory leak issue.

In the current implementation, the reference chain is approximately as follows:

LogBoxModule -> LogBoxDialogSurfaceDelegate -> mReactRootView -> ReactSurfaceView -> context (current Activity)

image

The issue arises because the current Activity reference is passed to ReactSurfaceImpl via the constructor during the creation of the View. However, the reference chain is only broken when the hide() method is called in LogBoxDialogSurfaceDelegate. Unfortunately, hide() is not always guaranteed to be called, which can lead to a memory leak.

When the ReactActivity exits and returns to another native Activity, LeakCanary (the memory leak detection tool) captures this memory leak.

If you need additional details, such as reproduction steps or screenshots from the memory leak detection tool, feel free to ask! 😊

@facebook-github-bot
Copy link
Contributor

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@cortinico cortinico left a comment

Choose a reason for hiding this comment

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

Thanks for sending this over @fannnzhang

Code looks good to me, but I'd like to do more testing around it before we can merge it

Comment on lines -26 to -30
/**
* LogBoxModule can be rendered in different surface. By default, it will use LogBoxDialog to wrap
* the content of logs. In other platform (for example VR), a surfaceDelegate can be provided so
* that the content can be wrapped in custom surface.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we not lose this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course, I've committed the changes to restore the deleted comment. Please review the comment to ensure it's correct.

@facebook-github-bot
Copy link
Contributor

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Jul 5, 2024
@facebook-github-bot
Copy link
Contributor

@cortinico merged this pull request in 0847384.

Copy link

github-actions bot commented Jul 5, 2024

This pull request was successfully merged by @fannnzhang in 0847384.

When will my fix make it into a release? | How to file a pick request?

@harsh-rksv
Copy link

@fannnzhang is the issues similar to this? #45217

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. Merged This PR has been merged. 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 Dialog Management: ReactRootView not removed from View Tree when Dialog is not showing
5 participants