Repository navigation
fix(macos): copying to the clipboard no longer keeps every image in memory - #18
Merged
CubityFirst merged 1 commit intoNov 24, 2026
Merged
Conversation
…emory The output thread copies to the clipboard for as long as snapr runs, but it has no run loop, so nothing drained its autorelease pool. Each image copied left its pixels and the pasteboard's encoded copy in that pool until snapr quit: about twice the screenshot's size per copy, over a gigabyte after a few days of use. Each clipboard write now runs in its own pool.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On macOS, every image snapr copies to the clipboard stays in memory until snapr quits. With copy-to-clipboard on, that's about twice the screenshot's size per capture. A snapr 0.3.5 that had been running for 10 days was at a 1.7 GB footprint (2.4 GB at peak).
The cause is the output thread. It does every clipboard write for as long as snapr runs, but it has no run loop, so nothing ever drains its autorelease pool. arboard's
set_imagedoesn't open a pool of its own; its reads do. So what AppKit autoreleases while writing the image piles up in that pool:NSBitmapImageRep/CGImageholding the pixels;dispatch_data_tin a Foundation VM region.Now each clipboard write in
output.rs(image, text and file) runs in its own pool on macOS. Other platforms are unchanged.Proof:
leaks -autoreleasePoolson that process shows one thread whose pool starts with theNSPasteboard, followed by 169 copied images. 512 MB of pixels is held only by that pool.leaks -referenceTree='VM:.*Foundation.*'roots another 692 MB of encoded pasteboard data in the same pool.leaksfinds only 70 KB of true leaks, so this is memory still referenced, not memory forgotten.set_image20 times with a 2000×1500 image on a thread that stays alive, as the output thread does. Without a pool it reaches 494 MB and stays at 493 MB 15 s later. With one it settles to 40 MB, and it keeps falling as the allocator hands back freed memory. The repro was a scratch test, not committed.cargo clippy --locked --all-targets -- -D warningsandcargo test --lockedpass on macOS.