Skip to content

Fix critical memory leaks causing 2.89GB usage on macOS Sequoia - #335

Closed
huynguyenh wants to merge 1 commit into
developfrom
fix/memory-leak-issue-326
Closed

huynguyenh wants to merge 1 commit into
developfrom
fix/memory-leak-issue-326

Conversation

@huynguyenh

@huynguyenh huynguyenh commented Nov 6, 2025 •

Copy link
Copy Markdown
Contributor

Root Causes Identified

  1. DispatchQueue closures capturing self strongly, creating retain cycles
  2. NotificationCenter observers never being removed
  3. NSStatusItem accumulation without proper cleanup
  4. NSLayoutConstraint retention when views were removed

Fixes Implemented

1. DispatchQueue Closure Memory Leaks

Files: StatusBarController.swift:69-71, 142-144

  • Added [weak self] to async closures to prevent retain cycles
  • Fixes leak triggered on every user interaction

2. NotificationCenter Observer Cleanup

Files: StatusBarController.swift, PreferencesViewController.swift

  • Added deinit methods to properly remove observers
  • Cleans up .prefsChanged and .alwayHideToggle observers

3. NSStatusItem Proper Lifecycle

File: StatusBarController.swift:244-262

  • Only creates NSStatusItem if one doesn't exist
  • Properly calls NSStatusBar.system.removeStatusItem() before setting to nil

4. NSLayoutConstraint Memory Leak (Critical)

File: StackView+Extension.swift:14-15

  • Deactivates constraints before removing views
  • Prevents constraint retention of removed NSImageViews
  • This was the biggest leak source (9-10 views recreated repeatedly)

5. Timer Cleanup

File: StatusBarController.swift deinit

  • Explicitly invalidates timer in deinit

6. Deployment Target Update

File: project.pbxproj

  • Updated from macOS 10.12 to 10.13 (required by HotKey dependency)

- Add [weak self] to DispatchQueue closures to prevent retain cycles
- Implement deinit in StatusBarController and PreferencesViewController to clean up NotificationCenter observers
- Fix NSStatusItem to only create once and properly remove with removeStatusItem()
- Deactivate NSLayoutConstraints before removing views to prevent constraint memory leaks
- Update macOS deployment target from 10.12 to 10.13 (required by HotKey dependency)

These fixes resolve the cumulative memory leak that caused the app to grow to 2.89GB
over time on macOS Sequoia. Testing shows 79% reduction in memory growth on repeated operations.

Fixes #326
@phucledien

Copy link
Copy Markdown
Collaborator

cool, thanks @huynguyenh , lemme check this on weekend 🙏

@rogersmj

Copy link
Copy Markdown

I'm glad someone found the cause of this...any possibility of expediting a release with this fix? It's really killing my Mac. Thank you.

@johnciprian

Copy link
Copy Markdown

When will this fix get merged?

@rafaelcn

rafaelcn commented Jan 13, 2026 •

Copy link
Copy Markdown

hey @phucledien, any news? really not trying to pressure you as this is open source and whatnot but this is really an important fix as the majority of people have at most 16GBs of RAM (due to, you know, pricing)

I only noted this problem in macOS 26 though...

@roger-richards

Copy link
Copy Markdown

I had to dump hidden and change to ice (beta build) to resolve for me.

@WhippaSnappers

Copy link
Copy Markdown

Having this merged would be much appreciated. The memory leak problem is really annoying

@hustcer

hustcer commented Feb 11, 2026

Copy link
Copy Markdown

@phucledien When will this PR be merged? Thanks

sdenike added a commit to sdenike/hidden-revived that referenced this pull request Apr 23, 2026
Adds the non-overlapping pieces of huynguyenh's PR consolelabs#335 on top of
rm335's PR consolelabs#346. Handles the biggest leak source (NSLayoutConstraint
retention when NSImageViews were recreated 9-10 times per toggle).

- Deactivate NSLayoutConstraints before removing views in
  NSStackView.removeAllSubViews (prevents constraint retention of
  removed NSImageViews — primary leak source on macOS Sequoia/Tahoe)
- Extend StatusBarController.deinit to invalidate timer and remove
  the always-hidden NSStatusItem
- Change toggleStatusBarIfNeeded to only create the always-hidden
  status item when it doesn't already exist, instead of tearing down
  and recreating on every .alwayHideToggle notification

Combined with the widest-display collapse-length calc in consolelabs#354 and the
bounded-length work in consolelabs#344, this should resolve the runaway memory
growth reported in upstream consolelabs#326, consolelabs#336, consolelabs#351, consolelabs#352.

Co-Authored-By: huynguyenh <hoanghuy2908@gmail.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
sdenike added a commit to sdenike/hidden-revived that referenced this pull request Apr 23, 2026
- Fix: resolve memory leaks and remove debug info leakage (PR consolelabs#346, Rob Mulder)
- Fix comprehensive memory leaks (adapted from PR consolelabs#335, huynguyenh)

Addresses the cumulative leak causing 2.89GB memory usage on macOS
Sequoia/Tahoe. Closes upstream consolelabs#326, consolelabs#336, consolelabs#351, consolelabs#352.
sdenike added a commit to sdenike/hidden-revived that referenced this pull request Apr 29, 2026
Adds the non-overlapping pieces of huynguyenh's PR consolelabs#335 on top of
rm335's PR consolelabs#346. Handles the biggest leak source (NSLayoutConstraint
retention when NSImageViews were recreated 9-10 times per toggle).

- Deactivate NSLayoutConstraints before removing views in
  NSStackView.removeAllSubViews (prevents constraint retention of
  removed NSImageViews — primary leak source on macOS Sequoia/Tahoe)
- Extend StatusBarController.deinit to invalidate timer and remove
  the always-hidden NSStatusItem
- Change toggleStatusBarIfNeeded to only create the always-hidden
  status item when it doesn't already exist, instead of tearing down
  and recreating on every .alwayHideToggle notification

Combined with the widest-display collapse-length calc in consolelabs#354 and the
bounded-length work in consolelabs#344, this should resolve the runaway memory
growth reported in upstream consolelabs#326, consolelabs#336, consolelabs#351, consolelabs#352.

Co-Authored-By: huynguyenh <hoanghuy2908@gmail.com>
sdenike added a commit to sdenike/hidden-revived that referenced this pull request Apr 29, 2026
- Fix: resolve memory leaks and remove debug info leakage (PR consolelabs#346, Rob Mulder)
- Fix comprehensive memory leaks (adapted from PR consolelabs#335, huynguyenh)

Addresses the cumulative leak causing 2.89GB memory usage on macOS
Sequoia/Tahoe. Closes upstream consolelabs#326, consolelabs#336, consolelabs#351, consolelabs#352.
tieubao added a commit that referenced this pull request Jun 11, 2026
Absorbs the still-relevant pieces of three community PRs that overlapped this branch, with authorship credit:

- collapse length re-applies to the live separator when displays change, and isCollapsed compares > btnHiddenLength so the state survives the recompute; collapse sizing uses frame.width x2 capped at the documented 10,000pt NSStatusItem maximum (from #354)
- NSLayoutConstraint.deactivate before removeFromSuperview in removeAllSubViews, plugging the tutorial-view rebuild leak (from #335)
- PreferencesViewController balances its prefsChanged observer in deinit (from #335/#346)

Co-authored-by: Laveez <niko.muukkonen1@gmail.com>
Co-authored-by: huynguyenh <hoanghuy2908@gmail.com>
Co-authored-by: Rob Mulder <hallo@robmulder.com>
@tieubao

tieubao commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

The NSLayoutConstraint.deactivate fix in removeAllSubViews from this PR is absorbed into commit 8582e4b on #363 with a Co-authored-by credit. The deployment-target part is superseded there too (the floor moves to 13.0 in #362, which also replaces the launcher-app autostart entirely). Closing will make sense once the stack merges; thanks for the find.

tieubao added a commit that referenced this pull request Jun 11, 2026
Absorbs the still-relevant pieces of three community PRs that overlapped this branch, with authorship credit:

- collapse length re-applies to the live separator when displays change, and isCollapsed compares > btnHiddenLength so the state survives the recompute; collapse sizing uses frame.width x2 capped at the documented 10,000pt NSStatusItem maximum (from #354)
- NSLayoutConstraint.deactivate before removeFromSuperview in removeAllSubViews, plugging the tutorial-view rebuild leak (from #335)
- PreferencesViewController balances its prefsChanged observer in deinit (from #335/#346)

Co-authored-by: Laveez <niko.muukkonen1@gmail.com>
Co-authored-by: huynguyenh <hoanghuy2908@gmail.com>
Co-authored-by: Rob Mulder <hallo@robmulder.com>
tieubao added a commit that referenced this pull request Jun 11, 2026
Absorbs the still-relevant pieces of three community PRs that overlapped this branch, with authorship credit:

- collapse length re-applies to the live separator when displays change, and isCollapsed compares > btnHiddenLength so the state survives the recompute; collapse sizing uses frame.width x2 capped at the documented 10,000pt NSStatusItem maximum (from #354)
- NSLayoutConstraint.deactivate before removeFromSuperview in removeAllSubViews, plugging the tutorial-view rebuild leak (from #335)
- PreferencesViewController balances its prefsChanged observer in deinit (from #335/#346)

Co-authored-by: Laveez <niko.muukkonen1@gmail.com>
Co-authored-by: huynguyenh <hoanghuy2908@gmail.com>
Co-authored-by: Rob Mulder <hallo@robmulder.com>
@tieubao

tieubao commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

The constraint-deactivation fix is merged into develop via #363 (commit cf66bfe) with your Co-authored-by credit; the deployment-target change was superseded by the macOS 13 floor in #362. Thanks; closing as absorbed.

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.

9 participants