From 938f008374ede10eb11bc0ca869b21bd66dff02a Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 12 Feb 2025 17:30:50 +0100 Subject: [PATCH] fix(applet): set mmap threshold to lower memory usage --- cosmic-files-applet/src/main.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cosmic-files-applet/src/main.rs b/cosmic-files-applet/src/main.rs index a724601..1337a5a 100644 --- a/cosmic-files-applet/src/main.rs +++ b/cosmic-files-applet/src/main.rs @@ -1,6 +1,28 @@ mod file_manager; +/// Access glibc malloc tunables. +#[cfg(target_env = "gnu")] +mod malloc { + use std::os::raw::c_int; + + const M_MMAP_THRESHOLD: c_int = -3; + + extern "C" { + fn mallopt(param: c_int, value: c_int) -> c_int; + } + + /// Prevents glibc from hoarding memory via memory fragmentation. + pub fn limit_mmap_threshold() { + unsafe { + mallopt(M_MMAP_THRESHOLD, 65536); + } + } +} + fn main() -> Result<(), Box> { + #[cfg(target_env = "gnu")] + malloc::limit_mmap_threshold(); + //TODO: move file manager service to its own daemon? let _conn_res = zbus::blocking::connection::Builder::session()? .name("org.freedesktop.FileManager1")?