Skip to content

fix: snapr no longer builds up memory the longer it runs - #20

Merged
CubityFirst merged 3 commits into
mainfrom
fix/prevent-memory-leaks
Nov 24, 2026
Merged

CubityFirst merged 3 commits into
mainfrom
fix/prevent-memory-leaks

Conversation

@CubityFirst

@CubityFirst CubityFirst commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

snapr lives in the tray for days, so anything that grows with each capture, recording or click adds up. A review of everything that adds to memory found eight places where that happened. The worst was the Recent page: every preview you looked at stayed in memory, up to about 9 MB each, for as long as the window was open.

What leaked, and the fix

  • Recent page: every large preview you viewed stayed loaded, as did thumbnails of screenshots that had dropped off the 200-item list. Previews that finished loading after their screenshot was removed were also put back. Now only the preview on screen is kept, thumbnails go with their screenshots, and late loads are dropped.
  • Closed windows' GPU memory: a finished capture's frozen screens (one per monitor, about 33 MB each at 4K) stayed on the GPU until another snapr window drew, because wgpu only frees memory when it next checks on the GPU's work. snapr now has it check whenever a capture, pin, toast or main window closes, counting each kind separately so a capture that turns into a pin is noticed. A lost GPU can make that check panic, so the panic is caught rather than taking the tray down.
  • Recording with two sound sources: if one source stopped mid-recording (pw-record exiting, say), the other's audio piled up at about 384 KB a second until you stopped. The stopped source now counts as silence until the end. A new test covers this.
  • Recording on X11, GNOME/KDE Wayland and macOS: full-screen frames that arrived just before stopping stayed queued until the next recording on that monitor. They're now dropped when recording stops.
  • FFmpeg recordings with sound: the raw audio file (about 23 MB a minute) was never deleted from the temp folder.
  • macOS: decoding a video's thumbnail on Recent's background thread kept its decoder objects until the window closed. This is the same autorelease-pool problem as fix(macos): copying to the clipboard no longer keeps every image in memory #18, and each decode now runs in its own pool.
  • macOS and Linux: each open/xdg-open used to open files, links and folders stayed behind as a zombie process. They're now waited for.
  • Linux portal hotkeys: a refused or cancelled shortcut binding left its session open in the portal. It's now closed.

Not changed, on purpose

None of these grow with use, so I left them out of this PR:

  • the text tool's font cache: bounded by the fonts you've tried
  • undo history: freed when the editor closes
  • the audio backlog when the encoder can't keep up with real time
  • the one cached redaction image
  • an unlikely error path in portal binding

Testing

  • Windows: cargo check, cargo +1.99.1 clippy --all-targets -D warnings and cargo test (189 passed) all pass locally.
  • Linux: cargo check --all-targets passes in WSL Ubuntu.
  • CI: tests and clippy pass on Windows, macOS and Ubuntu. macOS couldn't be built locally, so CI is its only check.
  • Not tested: actually running the app to measure memory. The fixes come from reading the code.

Council review

Rounds: 3

Delegate Round 1 Round 2 Round 3
Claude Fable 5.2 8/10 8/10 9/10
Gemini 4 Neon 6/10 8/10 10/10
GPT-6.1 Sol 6/10 9/10 10/10

Round 1 scored the app's existing memory hygiene. Rounds 2 and 3 scored this change.

A council review (Fable, Neon, Sol 6.1) of everything that adds to memory
in the long-running tray process turned up these:

- Recent: each large preview (up to 1920x1200, ~9 MB) stayed cached for
  as long as the window was open, as did the thumbnails of items that fell
  off the 200-item list, and loads finishing after their item was removed
  were put back. Only the shown preview is kept now, thumbnails go with
  their items, and stale loads are dropped.
- Windows that close (capture overlays, pins, the toast) left their last
  frames' GPU textures, a frozen screen per monitor, until the next window
  drew: wgpu only frees them when it checks on submitted work. snapr now
  has it check once a window has closed.
- Recording: a sound source that ended early (pw-record exiting, say)
  stopped padding silence, so the mixer, which mixes only what every
  source has delivered, held the others' samples for the rest of the
  recording (~384 KB/s). It stays silent until the end instead.
- Recording on X11, GNOME/KDE Wayland and macOS: frames that arrived just
  before stopping stayed queued with the cached recorder until the next
  recording on that monitor. They're dropped on stopping.
- FFmpeg recordings with sound left their raw audio (~23 MB a minute) in
  the temp folder.
- macOS: decoding a video in the thumbnail thread, which has no run loop,
  left its asset and reader in an autorelease pool until the window
  closed. The whole read now runs in its own pool.
- macOS and Linux: open/xdg-open, run to open files, links and folders,
  stayed as zombie processes. They're waited for now.
- Linux portal hotkeys: a session whose shortcuts were refused or
  cancelled stayed open in the portal; it's closed.
From the council's second round:

- Count the windows by kind: a capture that ends in a pin opens the pin
  before the capture's windows close, so the total didn't drop and the
  frozen screens stayed.
- Check last in `about_to_wait`, after the toast may have timed out
  there; on Linux nothing else wakes the loop once it has gone.
- wgpu panics on poll errors other than a timeout (a lost device, say),
  where drawing only fails quietly. Don't let that take the tray down.
@CubityFirst
CubityFirst merged commit 2076429 into main Nov 24, 2026
4 checks passed
@CubityFirst
CubityFirst deleted the fix/prevent-memory-leaks branch November 25, 2026 15:26
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.

1 participant