Repository navigation
Fix unbounded memory growth when the app runs for hours - #122
Closed
travisketchum wants to merge 3 commits into
Closed
travisketchum wants to merge 3 commits into
travisketchum wants to merge 3 commits into
Conversation
- Time-box BLE scans and drop AllowDuplicates; saved lights reconnect without scanning, and a short scan runs only while one is missing - Break DeviceViewObject retain cycle (weak self in bind closures, remove the no-op 10s timer) - Free AudioSpectrogram's manually allocated buffers and stop stacking volume listeners - Clean up legacy MCP SSE sessions when the client disconnects Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013kitTs6K338t9YLbyktXqU
Background scans are now time-boxed, so give the user a manual way to start a fresh scan if a saved light doesn't reconnect. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013kitTs6K338t9YLbyktXqU
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013kitTs6K338t9YLbyktXqU
Author
|
Closing this. The out-of-memory crashes I saw turned out to come from another app, so I’d rather not push these changes based on that. |
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.
Problem
Left running for several hours, NeewerLite's memory grows until macOS runs out of memory. On a 64 GB M1 Max, other apps start failing with out-of-memory errors.
Cause
The main cause is Bluetooth scanning that never stops:
centralManagerDidUpdateState starts an unfiltered scan at power-on, and nothing ever stops it.
scanAction always ends by restarting the scan with CBCentralManagerScanOptionAllowDuplicatesKey: true, and the Scan view's Stop button doesn't call stopScan().
Together, these deliver every advertisement from every nearby BLE device for as long as the app is open. CoreBluetooth keeps a CBPeripheral for every device it sees, and phones, watches and AirTags rotate their addresses, so that set never stops growing. peripheralInvalidCache was never pruned either.
Changes
Time-boxed scanning (AppDelegate.swift): startScanning(duration:) and stopScanning() replace the open-ended scans, and AllowDuplicates is gone. A scan stops after 60 s, or as soon as every saved light is connected. A Scan-for-new-lights scan still runs until the user presses Stop. Known lights reconnect through connect(), which doesn't need a scan. The existing 10 s keep-alive timer starts a 60 s scan, at most once every 5 minutes, only while a saved light has never been found.
Rescan for Lights: a new item in the menu-bar menu, just above Quit, that runs a one-off scan for saved lights. It is localized in de, es, fr, ja, ko and zh-Hans.
DeviceViewObject retain cycle: the Observable.bind closures now capture [weak self]. Before, each view object and its light were never freed. This also removes the no-op 10 s timer.
AudioSpectrogram: deinit now frees the manually allocated filterBank and sgemmResult buffers, which leaked every time Listen was turned off. startVolumeListener() now removes the previous CoreAudio listener first, so they don't stack up.
Legacy MCP /sse sessions (Server.swift): sessions are now removed when the client disconnects. A 30 s keepalive comment surfaces dead connections, a write error ends the session, and its MCP context is released. Stale session IDs are dropped when a context is re-bound.
Testing
Builds with Xcode 27.1, and all 237 unit tests pass.
Ran the patched build for about 23.5 hours on macOS (M1 Max). Memory peaked at 106 MB and sat at 86 MB at the end, with no steady growth. Before this change, the same setup exhausted 64 GB within hours.
Lights respond immediately to on/off throughout the day, with no manual rescan needed.