Repository navigation
Pause the open page and trim memory while the window is off screen #5767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mathew Newman (Thenewmanator15)
wants to merge
6
commits into
Devolutions:main
Choose a base branch
from
Thenewmanator15:bg/pause-and-idle-trim
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16441f2
Pause the open page's work while the window is off screen
web-flow 5864a87
Hand memory back to the OS once the app is idle in the background
web-flow 9062dea
Return freed native memory to the system when trimming on Linux
web-flow ea7b4ff
Fix formatting so the CI format gate passes
Thenewmanator15 39f04bc
Keep the user's place when the window is only minimized
Thenewmanator15 e8a1ac2
Merge main into bg/pause-and-idle-trim
Thenewmanator15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| namespace UniGetUI.Core.Classes.Tests; | ||
|
|
||
| public sealed class IdleMemoryTrimmerTests : IDisposable | ||
| { | ||
| private readonly Func<bool> _originalIsAppInBackground = IdleMemoryTrimmer.IsAppInBackground; | ||
| private readonly TimeSpan _originalQuietPeriod = IdleMemoryTrimmer.QuietPeriod; | ||
|
|
||
| public void Dispose() | ||
| { | ||
| IdleMemoryTrimmer.IsAppInBackground = _originalIsAppInBackground; | ||
| IdleMemoryTrimmer.QuietPeriod = _originalQuietPeriod; | ||
| } | ||
|
|
||
| private static async Task<bool> WaitForTrimCountAsync(int expected, int timeoutMs) | ||
| { | ||
| var watch = System.Diagnostics.Stopwatch.StartNew(); | ||
| while (watch.ElapsedMilliseconds < timeoutMs) | ||
| { | ||
| if (IdleMemoryTrimmer.TrimCount >= expected) | ||
| return true; | ||
| await Task.Delay(20); | ||
| } | ||
| return IdleMemoryTrimmer.TrimCount >= expected; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TrimsOnceTheAppHasBeenQuietInTheBackground() | ||
| { | ||
| IdleMemoryTrimmer.IsAppInBackground = static () => true; | ||
| IdleMemoryTrimmer.QuietPeriod = TimeSpan.FromMilliseconds(50); | ||
| int before = IdleMemoryTrimmer.TrimCount; | ||
|
|
||
| IdleMemoryTrimmer.Request(); | ||
|
|
||
| Assert.True(await WaitForTrimCountAsync(before + 1, 5000)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DoesNotTrimWhileTheAppIsInTheForeground() | ||
| { | ||
| IdleMemoryTrimmer.IsAppInBackground = static () => false; | ||
| IdleMemoryTrimmer.QuietPeriod = TimeSpan.FromMilliseconds(50); | ||
| int before = IdleMemoryTrimmer.TrimCount; | ||
|
|
||
| IdleMemoryTrimmer.Request(); | ||
| await Task.Delay(500); | ||
|
|
||
| Assert.Equal(before, IdleMemoryTrimmer.TrimCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ABurstOfRequestsTrimsOnlyOnce() | ||
| { | ||
| IdleMemoryTrimmer.IsAppInBackground = static () => true; | ||
| IdleMemoryTrimmer.QuietPeriod = TimeSpan.FromMilliseconds(300); | ||
| int before = IdleMemoryTrimmer.TrimCount; | ||
|
|
||
| for (int i = 0; i < 25; i++) | ||
| IdleMemoryTrimmer.Request(); | ||
|
|
||
| Assert.True(await WaitForTrimCountAsync(before + 1, 5000)); | ||
| await Task.Delay(700); | ||
| Assert.Equal(before + 1, IdleMemoryTrimmer.TrimCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TrimNowReleasesUnreachableMemory() | ||
| { | ||
| var garbage = new List<byte[]>(); | ||
| for (int i = 0; i < 64; i++) | ||
| garbage.Add(new byte[1024 * 1024]); | ||
| long withGarbage = GC.GetTotalMemory(forceFullCollection: false); | ||
| garbage.Clear(); | ||
|
|
||
| IdleMemoryTrimmer.TrimNow(); | ||
|
|
||
| Assert.True(GC.GetTotalMemory(forceFullCollection: false) < withGarbage - (32L * 1024 * 1024)); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| using System.Runtime; | ||
| using System.Runtime.InteropServices; | ||
| using UniGetUI.Core.Logging; | ||
|
|
||
| namespace UniGetUI.Core.Classes; | ||
|
|
||
| /// <summary> | ||
| /// Hands memory back to the operating system once the app has gone quiet in the background. | ||
| /// | ||
| /// The garbage collector only runs when the app allocates. A UniGetUI sitting in the tray | ||
| /// allocates almost nothing between update checks, so everything a check left behind (command | ||
| /// output, parsed package indexes, package lists it replaced) stayed in the heap as garbage | ||
| /// until the next check, and the heap stayed at its high-water mark the whole time. | ||
| /// | ||
| /// Work that leaves such garbage calls <see cref="Request"/> when it finishes. Once no further | ||
| /// request has arrived for <see cref="QuietPeriod"/>, and only while the app is in the | ||
| /// background, one compacting collection runs and the freed memory is released. Nothing happens | ||
| /// while the user is looking at the window, where a blocking collection would only cost a pause. | ||
| /// </summary> | ||
| public static partial class IdleMemoryTrimmer | ||
| { | ||
| /// <summary> | ||
| /// Whether nobody is looking at the app right now. The front-end sets it: a window that is | ||
| /// hidden or minimized, or a headless session. Defaults to "no", which disables trimming. | ||
| /// </summary> | ||
| public static Func<bool> IsAppInBackground { get; set; } = static () => false; | ||
|
|
||
| /// <summary>How long the app must stay quiet after the last request before trimming.</summary> | ||
| public static TimeSpan QuietPeriod { get; set; } = TimeSpan.FromSeconds(20); | ||
|
|
||
| private static long _lastRequest; | ||
|
|
||
| /// <summary>The number of trims performed since the process started.</summary> | ||
| public static int TrimCount => Volatile.Read(ref _trimCount); | ||
| private static int _trimCount; | ||
|
|
||
| /// <summary> | ||
| /// Asks for a trim once the app has been quiet for <see cref="QuietPeriod"/>. Cheap and safe | ||
| /// to call often, from any thread: a later request replaces the ones before it. | ||
| /// </summary> | ||
| public static void Request() | ||
| { | ||
| long request = Interlocked.Increment(ref _lastRequest); | ||
| TimeSpan delay = QuietPeriod; | ||
| _ = Task.Run(async () => | ||
| { | ||
| try | ||
| { | ||
| await Task.Delay(delay).ConfigureAwait(false); | ||
| if (request != Interlocked.Read(ref _lastRequest) || !IsAppInBackground()) | ||
| return; | ||
|
|
||
| TrimNow(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Logger.Warn("Could not trim memory while idle"); | ||
| Logger.Warn(ex); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /// <summary>Collects and compacts the whole heap now, and releases what that frees.</summary> | ||
| public static void TrimNow() | ||
| { | ||
| long before = GC.GetTotalMemory(forceFullCollection: false); | ||
| long committedBefore = GC.GetGCMemoryInfo().TotalCommittedBytes; | ||
| GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; | ||
| GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, blocking: true, compacting: true); | ||
| long after = GC.GetTotalMemory(forceFullCollection: false); | ||
| long committedAfter = GC.GetGCMemoryInfo().TotalCommittedBytes; | ||
| TrimNativeHeap(); | ||
| Interlocked.Increment(ref _trimCount); | ||
| Logger.Debug( | ||
| $"Trimmed memory while idle: managed heap {before / 1048576.0:F1} MB -> {after / 1048576.0:F1} MB" | ||
| + $" (committed {committedBefore / 1048576.0:F1} MB -> {committedAfter / 1048576.0:F1} MB)" | ||
| ); | ||
| } | ||
|
|
||
| // On Linux, memory freed by native code (the runtime itself, TLS, compression, child | ||
| // process plumbing) goes back to glibc's allocator, which keeps it for reuse rather than | ||
| // returning it to the system, so the process creeps up over many update checks. | ||
| // malloc_trim hands those free pages back. Other C libraries (musl) do not have it. | ||
| private static bool _nativeTrimUnavailable; | ||
|
|
||
| private static void TrimNativeHeap() | ||
| { | ||
| if (!OperatingSystem.IsLinux() || _nativeTrimUnavailable) | ||
| return; | ||
|
|
||
| try | ||
| { | ||
| _ = malloc_trim(0); | ||
| } | ||
| catch (Exception ex) when (ex is EntryPointNotFoundException or DllNotFoundException) | ||
| { | ||
| _nativeTrimUnavailable = true; | ||
| } | ||
| } | ||
|
|
||
| [LibraryImport("libc")] | ||
| private static partial int malloc_trim(nuint pad); | ||
| } |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.