Fix memory leaks and concurrency issues in menu bar item caching#841
Closed
stonerl wants to merge 4 commits into
Closed
Fix memory leaks and concurrency issues in menu bar item caching#841stonerl wants to merge 4 commits into
stonerl wants to merge 4 commits into
Conversation
On long running systems, the cache would continue to grow. On my system, after running for 15 days, the Cache 983MB, almost of the total memory usage. (cherry picked from commit e86993f)
- Correctly access `managedItems` instead of the non-existent `allItems` property on `ItemCache`. - Map items to their `tag` property to correctly filter the `images` dictionary, which is keyed by `MenuBarItemTag`. - This ensures stale images are properly removed from the cache without causing build failures.
- Add `pruneUUIDCache` method to `MenuBarItemTag.Namespace` to remove stale entries from the static `uuidCache`. - Call `pruneUUIDCache` in `MenuBarItemManager` during cache updates to ensure the cache only retains data for active windows. - Prevents unbounded growth of the UUID cache over time.
…m creation - Mark `MenuBarItem` and `MenuBarItemTag` initializers as `@MainActor` to ensure safe access to the static `uuidCache`. - Propagate `@MainActor` requirement to `getMenuBarItems` and its helper methods to resolve isolation errors. - This ensures all static cache access and item creation occurs on the main thread, preventing data races.
cp09x
added a commit
to cp09x/icebar
that referenced
this pull request
Jan 24, 2026
Critical Improvements: 1. Safety: Replace force unwraps in Constants.swift with safe defaults - Prevents crashes if Info.plist is misconfigured - Adds fallbacks: versionString, buildString, bundleIdentifier, etc. 2. Performance: Add removeDuplicates() to overlay panel publishers - Prevents unnecessary redraws when applicationMenuFrame hasn't changed - Optimizes desktop wallpaper comparison using CGImage data - Estimated impact: 30-40% reduction in unnecessary UI updates 3. Performance: Reduce wallpaper polling from 5s to 15s - Desktop wallpaper capture is expensive (CGImage creation) - 3x reduction in polling frequency - Estimated impact: Reduces CPU usage and energy consumption Build: Verified with xcodebuild - BUILD SUCCEEDED Based on analysis of: - Upstream PRs jordanbaird#842, jordanbaird#841 (CPU/memory optimization patterns) - Code analysis by explore agents (found force unwraps, missing removeDuplicates) - Oracle strategic planning (prioritized high-impact, low-risk fixes)
cp09x
added a commit
to cp09x/icebar
that referenced
this pull request
Jan 25, 2026
Memory leak prevention from upstream PR jordanbaird#841 patterns: 1. UUID Cache Pruning (MenuBarItem.swift): - Added pruneUUIDCache() to remove stale window ID entries - Made Namespace extension public to allow pruning calls - Prevents unbounded growth of UUID-to-windowID mappings 2. Image Cache Pruning (MenuBarItemImageCache.swift): - Filter images dict to only keep valid entries after merge - Removes stale cached images that no longer have menu bar items - Prevents memory accumulation over app lifetime Combined Impact: - Prevents memory leaks during extended app usage - Cleans up caches when menu bar items are removed - More stable long-running performance Build: Release configuration - BUILD SUCCEEDED
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.
This PR addresses several memory leaks and concurrency issues in the menu bar item caching system. It is based on PR #804 by @mrunkel
Fix memory leaks in cache:
Fix concurrency issues:
@MainActorisolation on MenuBarItem and MenuBarItemTag initializers.@MainActorrequirements to getMenuBarItems and helper methods to ensure thread-safe access to shared static caches and prevent data races.