Skip to content

[Medium] Audio decode allocates the whole file in memory (up to ~1 GiB) #10

Open

Description

@adilei

Severity

Medium — unbounded-ish memory use on long recordings (potential OOM).

Location

electron/audio/decode.ts:10 — const MAX_SAMPLES = 256 * 1024 * 1024; combined with the whole-file decode path in electron/audio/capture-preload.cjs.

Mechanism

Decoding loads the entire narration track into a single Float32Array. The cap is 256M samples × 4 bytes ≈ 1 GiB for one buffer (~4.6h at 16 kHz), and the intermediate decode holds the full file too. There is a DECODE_TIMEOUT_MS guard, but not a memory/streaming guard.

Impact

Long sessions can spike main/helper memory and OOM on lower-RAM machines.

Suggested fix

Decode/downmix incrementally (stream in CHUNK_SAMPLES-sized windows and append/flush), or lower the cap and fail fast with a clear message when exceeded rather than allocating up to ~1 GiB.

Regression test to add (npm test)

Unit test that a synthetic over-cap input is rejected/handled gracefully (no unbounded allocation), and that chunked decode reassembles a known short signal correctly. Deterministic; outside the evals/ scope.

Activity

  1. lizheng220 commented on Sep 11, 2026

    @lizheng220

    I looked at this with a patch in mind and want to check my reading first, because both halves of the suggested fix look like they're already in the tree. Happy to be corrected.

    Both suggested branches appear to be implemented

    Decode/downmix incrementally (stream in CHUNK_SAMPLES-sized windows and append/flush)

    That's what capture-preload.cjs already does in the audio:decode handler — it downmixes into chunkSamples-sized buffers, ships each over IPC, and yields to the event loop between them:

    for (let offset = 0; offset < outputLength; offset += chunkSamples) {
      const length = Math.min(chunkSamples, outputLength - offset);
      const output = new Float32Array(length);       // 1.0 MiB per chunk
      // ... downmix ...
      ipcRenderer.send("audio:decode-chunk", id, offset, output);
      await new Promise((resolve) => setTimeout(resolve, 0));
    }

    or lower the cap and fail fast with a clear message when exceeded rather than allocating up to ~1 GiB

    decode.ts already checks the cap before allocating — the bound test is in onMeta and the new Float32Array(length) only runs if it passes, so an over-cap track is rejected without ever allocating. What's open there is the threshold, not the ordering.

    What does look real, but differently from the write-up

    The whole decoded track genuinely exists twice, concurrently:

    1. the AudioBuffer from decodeAudioData() in the capture window — channels holds getChannelData() references for the whole downmix loop, and Web Audio has no streaming-decode entry point;
    2. one Float32Array(length) in the main process, because the ASR pipeline takes the full array.

    At the 16 kHz mono the decoder targets:

    recording samples Float32Array
    30 min 28,800,000 109.9 MiB
    1 h 57,600,000 219.7 MiB
    2 h 115,200,000 439.5 MiB
    4.66 h (MAX_SAMPLES) 268,435,456 1.00 GiB

    So a one-hour narration is ~220 MiB resident in each of two processes. That's a fair concern — I don't think the issue is wrong that there's a memory problem.

    Where I think the framing misleads is MAX_SAMPLES as the location. It's a validation bound, not an allocation target: actual usage tracks actual duration, so lowering it wouldn't reduce memory for any recording that already succeeds — it would only turn long recordings into failures earlier. (For scale, the encoded input is not a factor: at BITS_PER_SECOND = 24_000 an hour of narration is ~10.8 MB on disk.)

    Genuinely reducing peak memory means feeding the ASR pipeline in windows rather than one array, which changes the AsrPipeline input contract and how transcribe.ts stitches results — a much larger change than this issue is scoped for, and one I wouldn't want to attempt without your steer.

    What I'd suggest instead

    One small thing does look worth fixing on its own: when the cap is exceeded the user gets Decoded narration length is invalid: 268435457, which doesn't say "this recording is too long to transcribe". I'm happy to send a tiny PR that turns the over-cap case into an explicit, actionable message (and keeps the existing invalid-value path separate), if that's useful.

    Beyond that, the options as I see them are: retitle/rescope this toward the windowed-ASR change, lower MAX_SAMPLES as a deliberate product limit (which is a policy decision, not a memory fix), or close it as already mitigated by the chunked transfer plus the pre-allocation bound. I don't have a strong opinion on which — I mainly didn't want to send a patch that reimplements something that's already there.

    Happy to keep checking the rest of the findings from this batch the same way if it's useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions