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 fetch memory leak #44336

Closed

Conversation

huzhanbo1996
Copy link

@huzhanbo1996 huzhanbo1996 commented Apr 30, 2024

Summary:

Root cause of the fetch memory leak:

The fetch requests store its result inside Blob which memory is managed by BlobCollector. On the JS engine side,
the Blob is represented by an ID as JS string, and the GC don't know the size of the blob. So GC won't have interests to release the Blob.

Fix:

On iOS and Android, use setExternalMemoryPressure to acknowledge JS engine the size of Blob it holds.

Changelog:

[GENERAL] [FIXED] - fix fetch memory leak

Test Plan:

RepeatedlyFetch inside XHR example

@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 Apr 30, 2024
@huzhanbo1996
Copy link
Author

huzhanbo1996 commented Apr 30, 2024

This PR addresses issues #39100 #43647. @cortinico PTAL

@huzhanbo1996 huzhanbo1996 force-pushed the p/huzhanbo/fixBlobLeak branch from 21ec3cb to 005db2f Compare April 30, 2024 15:48
@analysis-bot
Copy link

analysis-bot commented Apr 30, 2024

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 19,478,635 +91
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 22,851,235 +148
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: 0383669
Branch: main

@@ -21,7 +21,7 @@ static void install(final ReactContext reactContext, final BlobModule blobModule
new Runnable() {
@Override
public void run() {
JavaScriptContextHolder jsContext = reactContext.getJavaScriptContextHolder();
JavaScriptContextHolder jsContext = reactContext.getCatalystInstance().getJavaScriptContextHolder();
Copy link
Contributor

Choose a reason for hiding this comment

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

This change might not be needed past #44360

Copy link
Author

Choose a reason for hiding this comment

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

Yes, change updated

Copy link
Author

Choose a reason for hiding this comment

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

@arushikesarwani94 Hi, PTAL again at the changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

@huzhanbo1996 huzhanbo1996 force-pushed the p/huzhanbo/fixBlobLeak branch from 005db2f to d1c98dd Compare May 2, 2024 01:48
@huzhanbo1996
Copy link
Author

@Sunbreak @NickGerleman @cortinico PTAL

@facebook-github-bot
Copy link
Contributor

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

@huzhanbo1996
Copy link
Author

Hi @NickGerleman, some facebook internal Lint/Check failed. Could you give me some guide to fix them?

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

@NickGerleman merged this pull request in c647950.

Copy link

This pull request was successfully merged by huzhanbo.luc in c647950.

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

@see2ever
Copy link

nice, waiting for this improvement!

@chanphiromsok
Copy link

how about using libs fetching api like axios ?

kosmydel pushed a commit to kosmydel/react-native that referenced this pull request Jun 11, 2024
Summary:
Root cause of the fetch memory leak:

The fetch requests store its result inside Blob which memory is managed by BlobCollector. On the JS engine side,
the Blob is represented by an ID as JS string, and the GC don't know the size of the blob. So GC won't have interests to release the Blob.

Fix:

On iOS and Android, use `setExternalMemoryPressure` to acknowledge JS engine the size of Blob it holds.

## Changelog:

[GENERAL] [FIXED] - fix fetch memory leak

Pull Request resolved: facebook#44336

Test Plan: `RepeatedlyFetch` inside `XHR` example

Reviewed By: javache

Differential Revision: D57145270

Pulled By: NickGerleman

fbshipit-source-id: afa53540e8563db4f9c6657f2dbbdff7bdfa66c0
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.

6 participants