Repository navigation
Pause the open page and trim memory while the window is off screen - #5767
Mathew Newman (Thenewmanator15) wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
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.
0171c24 to
4f89a4d
Compare
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.
4f89a4d to
39f04bc
Compare
|
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 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. |
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.SuspendForBackgroundcallsOnLeaveon the open page when the window is hidden or minimised, and drops the Help or Release Notes page when it is hidden.ResumeFromBackgroundundoes it. Settings pages are left alone so the user keeps their place.IdleMemoryTrimmerruns 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 callsmalloc_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:da6be7bThe log showed a trim after every check, for example
committed 65.5 MB -> 44.1 MBandcommitted 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
msedgewebview2processes to 0 within 3 seconds, and reopening the window rebuilt the page.Testing
dotnet formatgates pass, andUniGetUI.Core.Classes.TestsandUniGetUI.Testspass.Not tested: the Software Catalog page (the test machine has no catalog file) and Manager Health.