Skip to content

fix: memory-map local models instead of reading them into memory - #210

Open
DaveyEke wants to merge 2 commits into
margelo:mainfrom
DaveyEke:fix/memory-map-local-models
Open

DaveyEke wants to merge 2 commits into
margelo:mainfrom
DaveyEke:fix/memory-map-local-models

Conversation

@DaveyEke

@DaveyEke DaveyEke commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Continues #208 by @AlvYuste.

  • Local file:// models are now memory-mapped on Android and iOS instead of being read into memory.
  • Android file paths can now contain spaces and other URL-encoded characters.

Tested with a 205 MB model:

Before After
iPhone 16, load time 82–102 ms 8–10 ms
iPhone 16, memory after load 431 MB 20 MB
Pixel 7a, peak memory 1.73 GB 1.53 GB

AlvYuste and others added 2 commits November 1, 2026 21:23
Loading a `file://` model read the whole file into memory before handing it
to TFLite. On Android, `URL.readBytes()` fills a ByteArrayOutputStream that
grows to about twice the model's size in the Java heap, so a ~200 MB model
throws OutOfMemoryError against the common 256 MB heap limit. On iOS the
model was copied twice (into `Data`, then into the `ArrayBuffer`).

Local files are now memory-mapped and wrapped without a copy:
- Android maps the file read-only and wraps the direct ByteBuffer.
- iOS mmaps it copy-on-write and unmaps it when the ArrayBuffer is deleted.

`http(s)://` URLs and Android `res/raw` resources keep the existing paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants