Skip to content

Pause the open page and trim memory while the window is off screen - #5767

Open
Mathew Newman (Thenewmanator15) wants to merge 6 commits into
Devolutions:mainfrom
Thenewmanator15:bg/pause-and-idle-trim
Open

Mathew Newman (Thenewmanator15) wants to merge 6 commits into
Devolutions:mainfrom
Thenewmanator15:bg/pause-and-idle-trim

Conversation

@Thenewmanator15

@Thenewmanator15 Mathew Newman (Thenewmanator15) commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor
  • I have read the contributing guidelines, and I agree with the Code of Conduct.
  • Have you checked that there aren't other open pull requests for the same changes?
  • Have you tested that the committed code can be executed without errors?
  • Have you confirmed that this issue is caused by UniGetUI itself, and not by the package manager or the package involved?

Pages only stop their periodic work in OnLeave, which navigation calls. The page that was open when the window was hidden to the tray kept running: Software Catalog rescans every two seconds, Manager Health keeps refreshing, the log pages hold the whole log, and Help and Release Notes keep their web view.

This PR does two things:

  • MainWindowViewModel.SuspendForBackground calls OnLeave on the open page when the window is hidden or minimised, and drops the Help or Release Notes page when it is hidden. ResumeFromBackground undoes it. Settings pages are left alone so the user keeps their place.
  • IdleMemoryTrimmer runs one compacting GC 20 seconds after the app goes quiet, and only while the window is off screen. Package loads and the suspend above request it. On Linux it also calls malloc_trim.

Measurements

Windows 11, Debug builds started with --daemon, an update check every 2 minutes for 13 minutes. These figures are for this change together with my other background PRs, not for this one alone:

da6be7b With the changes
Private memory once idle about 240 MB, rising from 234 to 250 MB about 125 MB, back to the same level after each check

The log showed a trim after every check, for example committed 65.5 MB -> 44.1 MB and committed 45.6 MB -> 21.6 MB.

Releasing the web view on hide only frees the WebView2 processes with Avalonia.Controls.WebView 12.1.2, which is a separate PR. With both, hiding to the tray from Help took 6 msedgewebview2 processes to 0 within 3 seconds, and reopening the window rebuilt the page.

Testing

  • This branch alone: the Windows solution builds with 0 errors, both dotnet format gates pass, and UniGetUI.Core.Classes.Tests and UniGetUI.Tests pass.
  • The pause and trim code was written and first tested on Linux.

Not tested: the Software Catalog page (the test machine has no catalog file) and Manager Health.

GabrielDuf

This comment was marked as outdated.

@GabrielDuf Gabriel Dufresne (GabrielDuf) left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. I built the branch on Windows: the solution builds, the four IdleMemoryTrimmer tests pass, and both dotnet format gates pass. The OnLeave/OnEnter pairing through _suspendedForBackground holds up, including navigation while hidden and the suspend that runs before the window is first shown. Findings are inline.

The memory figures in the description were measured together with your other background PRs, so a measurement of this PR alone would help, as would a pass over the Software Catalog and Manager Health pages. The .NET Tests workflow has not run on this PR yet.

Pages only stopped their periodic work in OnLeave, which only navigation
calls. So whichever page was open when the window was hidden to the tray
(or minimized) kept running in the background for the rest of the
session: the Software Catalog rescanned the installed and upgradable
lists and stat'ed the catalog file every two seconds on the UI thread,
Manager Health kept refreshing, the log pages kept the full log in
memory, and Help / Release Notes kept a WebView2 process cluster alive.

The main window now suspends the current page (OnLeave) when it goes off
screen and resumes it (OnEnter) when it comes back. When the window is
hidden rather than minimized, the WebView2 pages are also released and
rebuilt on show. Settings pages are left alone: they do no background
work and their OnLeave would reset them to the settings home page.
The garbage collector only runs when the app allocates. A UniGetUI
sitting in the tray (or running headless) allocates almost nothing
between update checks, so whatever a check left behind stayed in the
heap as garbage until the next one, and the heap stayed at its
high-water mark the whole time. A memory snapshot of an idle headless
session showed the largest heap region full of dead data, such as the
complete PyPI file listings pip's update check had parsed minutes
earlier.

IdleMemoryTrimmer runs one compacting collection once the app has been
quiet for 20 seconds, and only while it is in the background (window
hidden or minimized, or a headless session), so the user never pays for
the pause. Package loaders request it when a load finishes, and the main
window when it goes off screen.

Measured on Linux with update checks every minute: the managed heap of a
headless session returns to ~4 MB after each check, where it previously
sat at 60-150 MB, and private memory holds at 41-53 MB where the same
build without this kept climbing past 260 MB.
Memory freed by native code goes back to glibc's allocator, which keeps
it for reuse rather than returning it to the system, so a long-running
session crept up over many update checks even with the managed heap
flat. The idle trim now also calls malloc_trim on Linux, and quietly
stops trying on C libraries that do not have it (musl).

Measured on a headless session checking for updates every minute:
private memory settles at 20-22 MB, where it previously ranged over
41-53 MB and drifted upwards.
The PR check fails on any whitespace or code-style diff; this is the part
of the formatting fix that belongs to the commits on this branch.
Review feedback on pausing the open page:

- Help lost its place on minimize and restore: resuming called OnEnter,
  and HelpPage.OnEnter navigates back to the address the page was opened
  with, discarding any link followed since. Help and Release Notes do no
  periodic work, so they no longer get OnLeave/OnEnter at all. They are
  still dropped when the window is hidden to the tray, and rebuilt when
  it is shown again.
- The log pages clear the log in OnLeave and reload it in OnEnter, which
  loses the scroll position. That now only happens when the window is
  hidden, not when it is minimized.
- ShowHelp(url) while the Help page was released (window hidden, Help
  still the current page) dropped the address: NavigateToAsync returns
  early for the same page and there was no page to navigate. The address
  is now kept and applied when the page is rebuilt.
- malloc_trim is declared with LibraryImport instead of DllImport, as the
  other libc imports in the repository are. UniGetUI.Core.Classes allows
  unsafe blocks for the generated marshalling code.
@Thenewmanator15

Copy link
Copy Markdown
Contributor Author

Thanks for building it and for the careful review. The four inline points are addressed in 39f04bc, and the branch is rebased on main after #5772 merged.

I measured this PR on its own as you asked: Windows 11, Debug build, started with --daemon, update check every 2 minutes for 13 minutes. Private memory settled at about 180 MB (176 to 210 MB, back down after each check), with a trim logged after each of the 6 checks. da6be7b measured the same way was about 240 MB, rising from 234 to 250 MB. That baseline is from before the rebase, so it is a few commits behind the branch.

I have not been able to check the Software Catalog page (my machine has no catalog file) or Manager Health. If you have a setup with a catalog I'd be glad of a second pair of eyes on that one.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants