react-native icon indicating copy to clipboard operation
react-native copied to clipboard

fix: Android OOM by eliminating intermediate buffer copies

Open DorianMazur opened this issue 1 month ago • 1 comments

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: https://github.com/facebook/react-native/pull/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:

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

DorianMazur avatar Dec 11 '25 14:12 DorianMazur

CC: @robhogan

DorianMazur avatar Dec 12 '25 11:12 DorianMazur

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

erlangparasu avatar Jan 01 '26 01:01 erlangparasu