From e540fc19f5c592236368fb19cf84a9ed54ae7540 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Tue, 4 Feb 2025 16:25:57 +0100 Subject: [PATCH] fix: set M_MALLOC_THRESHOLD to prevent memory fragmentation --- Cargo.lock | 1 + cosmic-applets/Cargo.toml | 1 + cosmic-applets/src/main.rs | 9 ++++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index dbac25a3..0bedd9d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1353,6 +1353,7 @@ dependencies = [ "cosmic-applet-time", "cosmic-applet-workspaces", "cosmic-panel-button", + "libc", "libcosmic", "tracing", "tracing-log", diff --git a/cosmic-applets/Cargo.toml b/cosmic-applets/Cargo.toml index 24d5931a..72dc0264 100644 --- a/cosmic-applets/Cargo.toml +++ b/cosmic-applets/Cargo.toml @@ -20,6 +20,7 @@ cosmic-applet-time = { path = "../cosmic-applet-time" } cosmic-applet-workspaces = { path = "../cosmic-applet-workspaces" } cosmic-applet-input-sources = { path = "../cosmic-applet-input-sources" } cosmic-panel-button = { path = "../cosmic-panel-button" } +libc = "0.2" libcosmic.workspace = true tracing.workspace = true tracing-subscriber.workspace = true diff --git a/cosmic-applets/src/main.rs b/cosmic-applets/src/main.rs index 93b3ec76..a4ed92ca 100644 --- a/cosmic-applets/src/main.rs +++ b/cosmic-applets/src/main.rs @@ -4,6 +4,13 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); fn main() -> cosmic::iced::Result { + // Prevents glibc from hoarding memory, such as cosmic-applet-minimize + // consuming 100s of megabytes on restoring a minimized window. + #[cfg(target_env = "gnu")] + unsafe { + libc::mallopt(libc::M_MMAP_THRESHOLD, 65536); + } + tracing_subscriber::fmt().with_env_filter("warn").init(); let _ = tracing_log::LogTracer::init(); @@ -32,6 +39,6 @@ fn main() -> cosmic::iced::Result { "cosmic-applet-workspaces" => cosmic_applet_workspaces::run(), "cosmic-applet-input-sources" => cosmic_applet_input_sources::run(), "cosmic-panel-button" => cosmic_panel_button::run(), - _ => return Ok(()), + _ => Ok(()), } }