Skip to content

Commit 91dadae

Browse files
committed
fix: set M_MMAP_THRESHOLD to prevent glibc memory fragmentation
1 parent 84a0bd7 commit 91dadae

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

cosmic-settings/src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ use ron::error::SpannedError;
2828
use serde::{Deserialize, Serialize};
2929
use tracing_subscriber::prelude::*;
3030

31+
/// Access glibc malloc tunables.
32+
#[cfg(target_env = "gnu")]
33+
mod malloc {
34+
use std::os::raw::c_int;
35+
36+
const M_MMAP_THRESHOLD: c_int = -3;
37+
38+
extern "C" {
39+
fn mallopt(param: c_int, value: c_int) -> c_int;
40+
}
41+
42+
/// Prevents glibc from hoarding memory via memory fragmentation.
43+
pub fn limit_mmap_threshold() {
44+
unsafe {
45+
mallopt(M_MMAP_THRESHOLD, 65536);
46+
}
47+
}
48+
}
49+
3150
#[derive(Parser, Debug, Serialize, Deserialize, Clone)]
3251
#[command(author, version, about, long_about = None)]
3352
#[command(propagate_version = true)]
@@ -140,6 +159,9 @@ impl CosmicFlags for Args {
140159
///
141160
/// Returns error if iced fails to run the application.
142161
pub fn main() -> color_eyre::Result<()> {
162+
#[cfg(target_env = "gnu")]
163+
malloc::limit_mmap_threshold();
164+
143165
color_eyre::install()?;
144166

145167
if std::env::var("RUST_SPANTRACE").is_err() {

0 commit comments

Comments
 (0)