[GH-ISSUE #4730] Server crashes with "JavaScript heap out of memory" on Android/Termux every few messages in large installs #2597

Closed
opened 2026-10-21 05:50:59 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Aug 7, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4730

Originally assigned to: @SpicyMarinara on GitHub.

From a user report (Android, Termux, staging): sending a message shows "Failed to fetch" in the chat UI, and the Termux terminal shows the backend dying with:

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

It happens every few messages. Their GC trace is the important clue — about 100 seconds after a fresh restart, a full Mark-Compact still had ~1010 MB live, against a heap limit of ~1 GB:

[14798:...] 100259 ms: Mark-Compact 1036.4 (1100.0) -> 1010.7 (1090.6) MB ... allocation failure; scavenge might not succeed
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

So this is not a leak — the install's data no longer fits in the heap V8 picked on that phone, and normal per-message allocation spikes push it over the edge.

Three things stack up

  1. The file-native store keeps every table in memory. All of data/storage/tables/*.json is loaded at boot and stays resident (packages/server/src/db/file-backed-store.ts). A long-running roleplay install (large message history, swipes, summary history, long-term memory) produces a permanent live set of many hundreds of MB.

  2. No launcher sets a heap size. There is no --max-old-space-size / NODE_OPTIONS anywhere in start.sh, start.bat, start-termux.sh, or the Android docs. Node falls back to V8's physical-memory heuristic, which on phones lands around 1 GB — far less than desktops get, on exactly the platform where installs are hardest to debug.

  3. Per-message allocation spikes are proportional to total table size, not message size.

    • Every insert/update shallow-copies the entire table array: const nextRows = target.map(cloneRow) (file-backed-store.ts:1100, also the update path at :1144). On a messages table with tens of thousands of rows, that's a full-array copy several times per send. (Pattern introduced in 37bb8f758, "Enforce file-native unique keys".)
    • The debounced flush serializes the whole dirty table into one contiguous string: JSON.stringify(rows) (file-backed-store.ts:1445 in saveFileSnapshots). For a big messages table that is a single allocation of hundreds of MB.

    With a ~800 MB baseline and a ~1 GB cap, whichever send happens to coincide with a flush dies. That's why it fails "every few messages" instead of deterministically.

Try it yourself

On any machine, artificially shrink the heap to phone scale and use a large profile (or generate one — a chat with tens of thousands of messages):

NODE_OPTIONS="--max-old-space-size=1024" pnpm start

Send messages in a large roleplay chat. Within a few sends, the flush after a generation OOMs the server the same way the Termux trace shows.

Suggested directions

  1. Cheapest, biggest win: set an explicit heap size in the launchers. start.sh / start.bat / start-termux.sh can export NODE_OPTIONS="--max-old-space-size=..." (respecting a user-provided value), sized from available RAM. Even a flat 2048 on Android would have absorbed this user's workload.
  2. Stream the flush. Write table snapshots as JSON Lines (row-by-row through the write stream) instead of one JSON.stringify of the whole table. The profile-ZIP exporter already writes JSONL per table (backup.routes.ts), so there's precedent and a reader for the format.
  3. Revisit the full-table clone per write. Copy-on-write of the whole array on every insert/update is O(table) allocation on hot per-send paths. Cloning only the affected rows (or sharding messages per chat) would bound it.

Workaround for affected users (worth adding to docs/TROUBLESHOOTING.md)

In Termux, before launching:

NODE_OPTIONS="--max-old-space-size=2048" PORT=7869 bash ./start.sh

(Use 3072 on phones with 8 GB+ RAM. Add the export to ~/.bashrc to make it permanent.)

Originally created by @kolacheee on GitHub (Aug 7, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4730 Originally assigned to: @SpicyMarinara on GitHub. From a user report (Android, Termux, staging): sending a message shows "Failed to fetch" in the chat UI, and the Termux terminal shows the backend dying with: ``` FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory ``` It happens every few messages. Their GC trace is the important clue — about 100 seconds after a fresh restart, a full Mark-Compact still had **~1010 MB live**, against a heap limit of ~1 GB: ``` [14798:...] 100259 ms: Mark-Compact 1036.4 (1100.0) -> 1010.7 (1090.6) MB ... allocation failure; scavenge might not succeed FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory ``` So this is not a leak — the install's data no longer fits in the heap V8 picked on that phone, and normal per-message allocation spikes push it over the edge. ## Three things stack up 1. **The file-native store keeps every table in memory.** All of `data/storage/tables/*.json` is loaded at boot and stays resident (`packages/server/src/db/file-backed-store.ts`). A long-running roleplay install (large message history, swipes, summary history, long-term memory) produces a permanent live set of many hundreds of MB. 2. **No launcher sets a heap size.** There is no `--max-old-space-size` / `NODE_OPTIONS` anywhere in `start.sh`, `start.bat`, `start-termux.sh`, or the Android docs. Node falls back to V8's physical-memory heuristic, which on phones lands around **1 GB** — far less than desktops get, on exactly the platform where installs are hardest to debug. 3. **Per-message allocation spikes are proportional to total table size, not message size.** - Every insert/update shallow-copies the **entire table array**: `const nextRows = target.map(cloneRow)` (`file-backed-store.ts:1100`, also the update path at `:1144`). On a messages table with tens of thousands of rows, that's a full-array copy several times per send. (Pattern introduced in `37bb8f758`, "Enforce file-native unique keys".) - The debounced flush serializes the **whole dirty table into one contiguous string**: `JSON.stringify(rows)` (`file-backed-store.ts:1445` in `saveFileSnapshots`). For a big messages table that is a single allocation of hundreds of MB. With a ~800 MB baseline and a ~1 GB cap, whichever send happens to coincide with a flush dies. That's why it fails "every few messages" instead of deterministically. ## Try it yourself On any machine, artificially shrink the heap to phone scale and use a large profile (or generate one — a chat with tens of thousands of messages): ```bash NODE_OPTIONS="--max-old-space-size=1024" pnpm start ``` Send messages in a large roleplay chat. Within a few sends, the flush after a generation OOMs the server the same way the Termux trace shows. ## Suggested directions 1. **Cheapest, biggest win: set an explicit heap size in the launchers.** `start.sh` / `start.bat` / `start-termux.sh` can export `NODE_OPTIONS="--max-old-space-size=..."` (respecting a user-provided value), sized from available RAM. Even a flat 2048 on Android would have absorbed this user's workload. 2. **Stream the flush.** Write table snapshots as JSON Lines (row-by-row through the write stream) instead of one `JSON.stringify` of the whole table. The profile-ZIP exporter already writes JSONL per table (`backup.routes.ts`), so there's precedent and a reader for the format. 3. **Revisit the full-table clone per write.** Copy-on-write of the whole array on every insert/update is O(table) allocation on hot per-send paths. Cloning only the affected rows (or sharding messages per chat) would bound it. ## Workaround for affected users (worth adding to docs/TROUBLESHOOTING.md) In Termux, before launching: ```bash NODE_OPTIONS="--max-old-space-size=2048" PORT=7869 bash ./start.sh ``` (Use 3072 on phones with 8 GB+ RAM. Add the `export` to `~/.bashrc` to make it permanent.)
bsakaguchi 2026-10-21 05:50:59 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Aug 7, 2026):

SpicyMarinara/Codex is implementing this in draft PR #4736 on agent/assigned-issues-4718-4734.

<!-- gh-comment-id:5222379728 --> @SpicyMarinara commented on GitHub (Aug 7, 2026): SpicyMarinara/Codex is implementing this in draft PR #4736 on `agent/assigned-issues-4718-4734`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#2597
No description provided.