Skip to content

Free the Settings window when it closes - #1930

Merged
rxhanson merged 3 commits into
rxhanson:mainfrom
zolferfigueiredo:fix/settings-window-memory
Oct 22, 2026
Merged

rxhanson merged 3 commits into
rxhanson:mainfrom
zolferfigueiredo:fix/settings-window-memory

Conversation

@zolferfigueiredo

Copy link
Copy Markdown
Contributor

Summary

Rectangle created the Settings window once and kept it for the life of the app. After the window was closed, every tab it had shown (the shortcuts outline, the Snap Areas, Behavior and App Settings SwiftUI forms) stayed in memory.

Now SettingsWindowController is the window's delegate. On close it tells AppDelegate to drop its reference and then clears the window's contentViewController, because AppKit keeps a closed window alive. The next open builds a fresh window.

Measurements

macOS 26.6, Apple Silicon, Release build. Settings opened, all four tabs visited, closed; repeated 3 times.

Before After
Idle at launch 12 MB 12 MB
Live heap after closing Settings 36.1 MB 14.6 MB
Footprint after closing Settings 64 MB 55 MB
SettingsTabViewController alive after close 1 0

Repeating open and close doesn't grow memory in either build.

What this does not change

  • Footprint after closing: it stays well above idle, because macOS doesn't hand back malloc pages that still hold a few surviving objects. The roughly 11 MB of survivors are one-time framework caches (Swift metadata, method caches, CoreUI and font caches).
  • Peak while switching tabs: it reaches about 150 MB, which is transient GPU memory during the tab change and is released right away.
  • Keyboard shortcuts: they don't leak. 900 window actions and 90 app switches stayed flat at 12 MB.

Test plan

  • Settings reopens after being closed, shows all 4 tabs, and switching tabs works
  • Unit tests pass (602)
  • Live heap and footprint measured before and after with heap and footprint

Built and tested on macOS 26.6 with Xcode 26.6. To compile with that SDK I stubbed out the macOS 27-only lines locally (the app icon and preferredImageVisibility); those local changes are not part of this PR.

The Settings window controller was created once and kept for the life of
the app, so every tab's SwiftUI and AppKit views stayed in memory after
the window was closed. Drop the window content after close and clear the
reference; the next open builds a fresh window.

Measured on macOS 26.6, Release build, all four tabs visited, then closed:
live heap 36 MB -> 14.6 MB, footprint 64 MB -> 55 MB. Idle at launch is
unchanged at 12 MB.
@rxhanson

Copy link
Copy Markdown
Owner

Thanks! I've been meaning to do this. One thing that I've been doing in my other apps is saving which tab it was open on, and navigating back to that tab when it opens. Would you be up for adding that in as part of this PR?

Save the selected Settings tab in UserDefaults when the user switches tabs
and select it again when the window is built, so closing or relaunching
lands back on the same tab instead of always on Shortcuts. The window title
and initial size follow the restored tab, measured from the tab view that is
already built rather than a throwaway one. An unknown saved value falls back
to Shortcuts. The key is UI state, so it is left out of settings export.
@zolferfigueiredo

Copy link
Copy Markdown
Contributor Author

Sure, it's done. Settings now remembers which tab was open and goes back to it the next time the window opens, including after quitting the app. The window title and size follow that tab, and if the saved value isn't a valid tab it falls back to Shortcuts. I kept it out of the settings export since it's window state rather than a preference.

@rxhanson

Copy link
Copy Markdown
Owner

Thanks! There's a bug with sizing the different tabs when reopening that's exposed by saving and restoring the tab, but I have a fix for it and I'll just commit it to this branch and merge it when I have it done.

…b sizes; also removed persistence between app restarts
@rxhanson
rxhanson merged commit 7c5c9c5 into rxhanson:main Oct 22, 2026
@rxhanson

Copy link
Copy Markdown
Owner

I ended up removing the persistence across app relaunches, as I preferred starting consistently at the shortcuts tab. Thanks again!

zolferfigueiredo added a commit to zolferfigueiredo/Rectangle that referenced this pull request Nov 5, 2026
Brings in v2.0.4 and the 19 commits after it. The Settings window fix landed upstream as rxhanson#1930 with more on top (remembering the last tab and window size), so SettingsWindowController.swift takes upstream's version. The fork's main now has the same files as upstream's.
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.

2 participants