react-native
react-native copied to clipboard
fix: Android OOM by eliminating intermediate buffer copies
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
BufferedSourceper 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-Statussafely.
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
CC: @robhogan
@DorianMazur #ask is there full repo to test this PR ? thanks