Skip to content

misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux

Tray: give memory back after Minimize too, and again 2 minutes after hiding (#72) (#151)

Ahmed Nefzaouic0bd0da4d8d8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5f3d568b..5ff4b775 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -320,6 +320,11 @@ Each release's notes on GitHub come from its section here (`## [X.Y.Z]`, see
### Fixed
+- **Less memory in the notification area, also after Minimize**
+ ([#72](https://github.com/anefzaoui/misterdownloadmanager/issues/72)): a window minimized to the
+ notification area (on by default) now gives its memory back too, as a closed one did; and both
+ give it back again 2 minutes after hiding, when the clock's ticks and a one-time allocation
+ have brought some back (then every 10 minutes, as before).
- **Screen readers see a window's contents the first time they read it**
([#131](https://github.com/anefzaoui/misterdownloadmanager/issues/131)): a window read for the
first time (Link grabber, Options, About...) showed only its title bar until it was drawn
diff --git a/ROADMAP.md b/ROADMAP.md
index 4aaa6afa..09669097 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -13,6 +13,8 @@ Everything below ships together, in one release.
### Done
+- ✅ Less memory in the notification area after Minimize too, and again 2 minutes after hiding
+ ([#72](https://github.com/anefzaoui/misterdownloadmanager/issues/72))
- ✅ Windows: draw with Direct3D 12 instead of OpenGL (**View → Draw with**), an option for now;
either one falls back to the other when it can't start
([#89](https://github.com/anefzaoui/misterdownloadmanager/issues/89))
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rs
index 3761638a..0beeb59c 100644
--- a/crates/app/src/app.rs
+++ b/crates/app/src/app.rs
@@ -251,10 +251,17 @@ enum Popup {
Node(Option<View>),
}
-/// Hidden to the tray, the window's memory is given back this long after it hid (its last frames
-/// done), then again this often while it stays hidden (the clock's ticks page some back in).
-const TRIM_AFTER_HIDE: Duration = Duration::from_secs(2);
-const TRIM_EVERY_HIDDEN: Duration = Duration::from_secs(600);
+/// Hidden to the tray, the window's memory is given back 2 s after it hid (its last frames done),
+/// again 2 minutes after it hid (the clock's ticks page some back in, about 5 MB in the first
+/// minute, and the process takes 4 MB more once, about 2 minutes after start), then every
+/// 10 minutes while it stays hidden. `done` is how many times it was given back since hiding.
+fn next_trim(done: u32) -> Duration {
+ match done {
+ 0 => Duration::from_secs(2),
+ 1 => Duration::from_secs(118),
+ _ => Duration::from_secs(600),
+ }
+}
/// Hand the memory the process isn't using now back to the system (#72).
fn trim_memory() {
@@ -439,8 +446,10 @@ pub struct App {
tray: Option<crate::tray::Tray>,
tray_limiter: Option<bool>,
hidden: bool,
- /// Hidden to the tray: when to hand the window's memory back to the system next (#72).
+ /// Hidden to the tray: when to hand the window's memory back to the system next (#72), and
+ /// how many times it was since the window hid.
trim_when_hidden: Option<Instant>,
+ trims_since_hidden: u32,
exiting: bool,
/// Clipboard watching (Options → General): the text last seen (outer None: not looked
/// yet) and when.
@@ -855,6 +864,7 @@ impl App {
tray_limiter: None,
hidden: false,
trim_when_hidden: None,
+ trims_since_hidden: 0,
exiting: false,
confirm_delete: None,
confirm_queue_delete: None,
@@ -2363,9 +2373,15 @@ impl App {
}
ctx.send_viewport_cmd(egui::ViewportCommand::Visible(false));
self.hidden = true;
- // Hidden, the window's memory sits unused until it shows again: give it back to the
- // system (#72). Windows pages it back in from RAM when it's touched again.
- self.trim_when_hidden = Some(Instant::now() + TRIM_AFTER_HIDE);
+ self.start_trimming();
+ }
+
+ /// Hidden, the window's memory sits unused until it shows again: give it back to the system
+ /// (#72), on the schedule of `next_trim`. Windows pages it back in from RAM when it's touched
+ /// again. Both ways into the tray (closing the window, and Minimize when it goes there).
+ fn start_trimming(&mut self) {
+ self.trims_since_hidden = 0;
+ self.trim_when_hidden = Some(Instant::now() + next_trim(0));
}
/// The window back from the tray (or from minimized), in front.
@@ -2488,6 +2504,7 @@ impl App {
tracing::info!("minimized to the notification area");
ctx.send_viewport_cmd(egui::ViewportCommand::Visible(false));
self.hidden = true;
+ self.start_trimming();
}
// Hidden, the window gets no paint messages: keep the clock (queues, scheduler,
// download limits) running.
@@ -2495,7 +2512,8 @@ impl App {
ctx.request_repaint_after(Duration::from_secs(1));
if self.trim_when_hidden.is_some_and(|t| Instant::now() >= t) {
trim_memory();
- self.trim_when_hidden = Some(Instant::now() + TRIM_EVERY_HIDDEN);
+ self.trims_since_hidden += 1;
+ self.trim_when_hidden = Some(Instant::now() + next_trim(self.trims_since_hidden));
}
}
}
@@ -7265,3 +7283,23 @@ mod referrer_tests {
assert_eq!(drive_file_behind_player(&cmd(folder, None)), None);
}
}
+
+#[cfg(test)]
+mod trim_tests {
+ use super::next_trim;
+ use std::time::Duration;
+
+ /// The tray gives memory back 2 s after hiding, 2 minutes after hiding, then every
+ /// 10 minutes (#72).
+ #[test]
+ fn hidden_memory_is_given_back_at_2_s_2_min_then_every_10_min() {
+ let mut at = Duration::ZERO;
+ let times: Vec<u64> = (0..4)
+ .map(|done| {
+ at += next_trim(done);
+ at.as_secs()
+ })
+ .collect();
+ assert_eq!(times, [2, 120, 720, 1320]);
+ }
+}