Skip to content

Engine: Drain autoreleased chunks on the feeder, pump and waveform threads - #552

Merged
kddlb merged 1 commit into
losnoco:mainfrom
alfishe:engine-autorelease-pools
Nov 9, 2026
Merged

kddlb merged 1 commit into
losnoco:mainfrom
alfishe:engine-autorelease-pools

Conversation

@alfishe

@alfishe alfishe commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #551.

The feeder and pump threads loop without an autorelease pool of their own, so every autoreleased object a pass makes (the Float32 chunks ChunkList's removeSamplesAsFloat32 / convertChunk hand back, and their data) lived until the thread ended. The waveform scanner drained only per decoded chunk, so a whole track's converted blocks piled up while it scanned.

This change wraps each of these in an autoreleasepool:

  • feed(chunk) in Feeder.run()
  • each pass() in Pump.run()
  • each converted block in WaveformScanner (the block body returns next / stop / done, so break and return true keep their meaning)

Tested on macOS 15.8.1 arm64 with a Debug build:

  • The footprint over 140 s of playback is flat at 130-135 MB; before, it grew about 1 MB/s.
  • The heap diff over 2 minutes of playback is about +0.3 MB (UI caches); leaks finds 880 bytes, all UI.
  • The device output, recorded with engineCaptureDirectory, is sample for sample equal to the decoder's PCM.

…reads

The feeder and pump threads run their loops with no autorelease pool of
their own, so every autoreleased object a pass makes (the Float32 chunks
ChunkList's removeSamplesAsFloat32 and convertChunk hand back, and their
data) lived until the thread ended: the footprint grew for as long as
playback went on, about 1 MB/s with a decoder that returns one ~20 ms
chunk per readAudio. The waveform scanner drained its pool only per
decoded chunk, so a whole track's converted blocks piled up while it
scanned (hundreds of MB for a long track of small chunks).

Wrap the feeder's feed(chunk), each pump pass, and each scanned block
in an autoreleasepool.
@viviea1

viviea1 commented Nov 9, 2026

Copy link
Copy Markdown

I ran into the same problem as #551: after long playback, memory kept growing until it filled the system's memory.

With this PR's changes, I played continuously for 1 hour and did not see the continuous memory growth. Memory usage stayed below 200 MB.

Setup

  • System: macOS 26.3.2, arm64
  • Before: official Cog 3868 (3c1af193)
  • Tested: Cog 3880 (af67a918) with this PR's changes (ddd4e70f)

kddlb added a commit that referenced this pull request Nov 9, 2026
#552 pooled only feed(chunk), so what a seek, a reset or a track change
autoreleased (opening the next decoder, its properties) still lived as
long as the feeder thread. Put the whole pass in the pool instead, with
the loop body moved into pass(), as the pump does.

Inside feed(), drain per 4096-frame block rather than per decoded
chunk, as the waveform scanner now does, so a decoder that returns
large chunks doesn't hold all their converted blocks at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kddlb
kddlb merged commit ddd4e70 into losnoco:main Nov 9, 2026
@kddlb

kddlb commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

With this PR's changes, I played continuously for 1 hour and did not see the continuous memory growth. Memory usage stayed below 200 MB.

Confirmed this as well. Thanks @alfishe and @viviea1.

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.

Engine: memory grows about 1 MB/s while playing (no autorelease pool on the feeder and pump threads)

3 participants