Skip to content

Fix: stream chunk uploads from disk to avoid OOM on large files#5

Open
mamont wants to merge 1 commit into
Gautham495:mainfrom
padelai:fix/stream-chunks-to-avoid-oom
Open

Fix: stream chunk uploads from disk to avoid OOM on large files#5
mamont wants to merge 1 commit into
Gautham495:mainfrom
padelai:fix/stream-chunks-to-avoid-oom

Conversation

@mamont

@mamont mamont commented Jun 9, 2026

Copy link
Copy Markdown

Summary

Fixes high memory usage/OOM during multipart uploads by streaming chunk data instead of loading entire chunks into memory.

Changes

  • Android: replace full ByteArray chunk allocation with a streaming OkHttp RequestBody.
  • iOS: replace full Data chunk allocation with buffered copy to the temporary upload file.
  • Keeps chunk boundaries, retry behavior, and multipart upload flow unchanged.

Validation

  • yarn typecheck
  • yarn lint
  • yarn test
  • yarn example android
  • yarn example ios
  • Tested real multipart upload with large file

Both upload paths read each part fully into memory before PUTting it:
Android allocated ByteArray(size) in readChunk, and iOS read the whole
chunk into Data before writing the temp file. With large chunk sizes and
several parts prepared in parallel, peak memory scaled with
chunkSize x parallelism and reliably got OOM-killed on lower-heap devices.

Replace both with a fixed 256 KB streaming copy:
- Android: a custom RequestBody (streamingFileChunkBody) seeks to the part
  offset and streams through a reusable 256 KB buffer. OkHttp may invoke
  writeTo() more than once on retry, so the file is reopened per call.
  Content-Length is now derived from contentLength().
- iOS: streamChunkToFile copies the part into the temp file through a
  256 KB buffer instead of buffering the whole chunk in Data.

Both fail fast on a short read so a truncated part can't corrupt the S3
multipart upload. Per-part memory is now the buffer size regardless of
chunk size.

Fix linter warnings
@mamont mamont force-pushed the fix/stream-chunks-to-avoid-oom branch from 09e1ced to 93f1c13 Compare June 9, 2026 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant