use lazy_static::lazy_static;
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use tokio::{spawn, time::sleep};
use tracing::{error, info};
lazy_static! {
static ref TRIM_TIMER: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>> = Arc::default();
}
pub fn schedule_trim() {
info!("Scheduling trim …");
let timer_arc = TRIM_TIMER.clone();
let Ok(mut guard) = timer_arc.lock() else {
error!("trim schedule mutex guard is poisoned");
return;
};
if let Some(handle) = guard.take() {
handle.abort();
}
let handle = spawn(async move {
sleep(Duration::from_secs(1)).await;
info!("Trim triggered.");
if trim_now() {
info!("Memory released.");
}
});
*guard = Some(handle);
}
#[cfg(target_os = "linux")]
fn trim_now() -> bool {
unsafe { libc::malloc_trim(0) != 0 }
}
#[cfg(not(target_os = "linux"))]
fn trim_now() -> bool {
false
}