Skip to content

fix: use Buffer.concat instead of Array.push for IDAT chunks to prevent OOM#83

Open
xbfld wants to merge 1 commit intofoliojs:masterfrom
xbfld:fix/idat-buffer-concat-oom
Open

fix: use Buffer.concat instead of Array.push for IDAT chunks to prevent OOM#83
xbfld wants to merge 1 commit intofoliojs:masterfrom
xbfld:fix/idat-buffer-concat-oom

Conversation

@xbfld
Copy link
Copy Markdown

@xbfld xbfld commented Feb 27, 2026

Closes #84

Problem

The IDAT chunk parser accumulated compressed image data by pushing individual bytes into a JavaScript Array. For large PNG files (100MB+), this created arrays with 100M+ elements. Since V8 stores each Smi element using 8 bytes on 64-bit platforms, a 107MB PNG would consume ~860MB of heap memory, causing V8 to crash with Fatal JavaScript invalid size error.

Solution

Replace the byte-by-byte Array.push approach with Buffer.from/slice to collect IDAT chunk data as Buffer slices, then Buffer.concat to merge them.

Before After
107MB IDAT ~860MB heap (JS Array) ~107MB heap (Buffer)
Result V8 crash Success

This PR description was written by Claude Code (claude-opus-4-6)

…nt OOM

The IDAT chunk parser accumulated compressed image data by pushing
individual bytes into a JavaScript Array. For large PNG files (100MB+),
this created arrays with 100M+ elements. Since V8 stores each Smi
element using 8 bytes on 64-bit platforms, a 107MB PNG would consume
~860MB of heap memory just for the JS Array backing store, far exceeding
the actual data size.

This caused V8 to crash with 'Fatal JavaScript invalid size error' when
the array's backing FixedArray exceeded V8's maximum allocation size
during capacity growth.

Replace the byte-by-byte Array.push approach with Buffer.from/slice to
collect IDAT chunk data as Buffer slices, then Buffer.concat to merge
them. This reduces memory usage from ~8x the data size to ~1x.

Before: 107MB IDAT data -> ~860MB heap (JS Array) -> V8 crash
After: 107MB IDAT data -> ~107MB heap (Buffer)   -> success
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.

OOM crash when decoding large PNG files - IDAT chunk accumulated as JS Array

2 participants