Skip to content

Conversation

@DorianMazur
Copy link
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
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
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.

2 participants