Skip to content

fix: Android OOM by eliminating intermediate buffer copies#54854

Closed
DorianMazur wants to merge 4 commits into
facebook:mainfrom
DorianMazur:fix/android-bundler
Closed

fix: Android OOM by eliminating intermediate buffer copies#54854
DorianMazur wants to merge 4 commits into
facebook:mainfrom
DorianMazur:fix/android-bundler

Conversation

@DorianMazur
Copy link
Copy Markdown
Contributor

@DorianMazur DorianMazur commented Dec 11, 2025

Summary:

During JS bundle downloads from Metro, the multipart stream reader was copying each chunk into a new Buffer before passing it to listeners. For large bundles, this resulted in elevated peak memory usage due to duplicating chunk data (Okio read buffer + intermediate Buffer copy, plus downstream buffering), which can exceed emulator heap limits for large bundles.
Example: #52818
Repro: #52797

Changes

  • Multipart parsing: pass a bounded BufferedSource per part (prevents reading past the part into the next boundary) and drain unread bytes after callbacks so listeners don’t need to fully consume the body.
  • BundleDownloader: keep streaming download behavior while restoring atomic writes (.tmp + rename) to avoid partial bundles on interruption.
  • Make Content-Type checks tolerant of parameters, parse X-Http-Status safely.

Changelog:

[ANDROID] [FIXED] - Reduced memory usage during JS bundle downloads by eliminating intermediate buffer copies

Test Plan:

  • Verified multipart bundle downloads work correctly with progress callbacks displayed
  • Verified non-multipart fallback path still functions
  • Verified error responses are handled correctly
  • Tested with large bundles (repro above) and confirmed reduced memory pressure and no crashes

@meta-cla meta-cla 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 Dec 11, 2025
@DorianMazur DorianMazur marked this pull request as ready for review December 11, 2025 16:03
@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 Dec 11, 2025
@DorianMazur
Copy link
Copy Markdown
Contributor Author

CC: @robhogan

@DorianMazur DorianMazur changed the title fix: reduce memory usage in bundle downloading by eliminating intermediate buffer copies fix: Android OOM by eliminating intermediate buffer copies Dec 16, 2025
@erlangparasu
Copy link
Copy Markdown

erlangparasu commented Jan 1, 2026

@DorianMazur #ask is there full repo to test this PR ? thanks

@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented Feb 12, 2026

@robhogan has imported this pull request. If you are a Meta employee, you can view this in D93102028.

@meta-codesync meta-codesync Bot closed this in 57eb56f Jun 2, 2026
@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @DorianMazur in 57eb56f

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

@facebook-github-tools facebook-github-tools Bot added the Merged This PR has been merged. label Jun 2, 2026
@react-native-bot react-native-bot added the Merged This PR has been merged. label Jun 2, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented Jun 2, 2026

@robhogan merged this pull request in 57eb56f.

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.

4 participants