From 70f6678271fbdb4a503dec36081017913faf3718 Mon Sep 17 00:00:00 2001 From: Ivan <87540740+Pecnikas@users.noreply.github.com> Date: Mon, 30 Nov 2026 21:51:07 +0500 Subject: [PATCH] perf(memory): release freed heap pages after closing a project or tabs (#7072) Closing a project or a batch of documents now calls release_free_pages once, 2 s after the last close, on a background thread. On macOS the freed pages are already reclaimable, so the footprint change is within noise; on Linux malloc_trim can return pages glibc would keep resident (not measured). Co-authored-by: Pecnikas <87540740+Pecnikas@users.noreply.github.com> --- crates/ide-core/src/memory.rs | 55 ++++++++++++++++++++++++++++++++++ crates/ide-host/src/lib.rs | 2 ++ crates/ide-host/src/ops/doc.rs | 1 + 3 files changed, 58 insertions(+) diff --git a/crates/ide-core/src/memory.rs b/crates/ide-core/src/memory.rs index e42e6fd6eb..18d6651b08 100644 --- a/crates/ide-core/src/memory.rs +++ b/crates/ide-core/src/memory.rs @@ -1,5 +1,35 @@ //! Returning freed heap pages to the OS. +use std::sync::Mutex; +use std::time::{Duration, Instant}; + +/// Like [`release_free_pages`], on a background thread once 2 s pass without another call: +/// closing a project or a batch of tabs frees memory in bursts, released once. +pub fn release_free_pages_soon() { + static DUE: Mutex> = Mutex::new(None); + debounce(&DUE, Duration::from_secs(2), release_free_pages); +} + +/// Runs `run` on a background thread once `delay` passes after the last call. +fn debounce(due: &'static Mutex>, delay: Duration, run: fn()) { + let idle = due.lock().unwrap().replace(Instant::now() + delay).is_none(); + if idle { + std::thread::spawn(move || { + loop { + let mut slot = due.lock().unwrap(); + let left = slot.map_or(Duration::ZERO, |at| at.saturating_duration_since(Instant::now())); + if left.is_zero() { + *slot = None; + drop(slot); + return run(); + } + drop(slot); + std::thread::sleep(left); + } + }); + } +} + /// Hands pages the allocator has freed but kept back to the OS. Call after a burst of /// short-lived allocations (a full index scan); until then they still count towards the /// process's memory footprint. @@ -18,3 +48,28 @@ pub fn release_free_pages() { libc::malloc_trim(0); } } + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; + + static DUE: Mutex> = Mutex::new(None); + static RUNS: AtomicUsize = AtomicUsize::new(0); + + fn count() { + RUNS.fetch_add(1, SeqCst); + } + + #[test] + fn a_burst_of_calls_runs_once_after_the_delay() { + for _ in 0..5 { + debounce(&DUE, Duration::from_millis(100), count); + std::thread::sleep(Duration::from_millis(20)); + } + assert_eq!(RUNS.load(SeqCst), 0); + std::thread::sleep(Duration::from_millis(400)); + assert_eq!(RUNS.load(SeqCst), 1); + release_free_pages_soon(); + } +} diff --git a/crates/ide-host/src/lib.rs b/crates/ide-host/src/lib.rs index c28147ae53..0f323dc36f 100644 --- a/crates/ide-host/src/lib.rs +++ b/crates/ide-host/src/lib.rs @@ -551,6 +551,8 @@ impl Host { if let Some(retired) = retired { let _ = retired.join(); } + // The project's buffers, index and caches are dropped: hand their pages back. + ide_core::memory::release_free_pages_soon(); }) } diff --git a/crates/ide-host/src/ops/doc.rs b/crates/ide-host/src/ops/doc.rs index b515315e44..e674c60959 100644 --- a/crates/ide-host/src/ops/doc.rs +++ b/crates/ide-host/src/ops/doc.rs @@ -26,6 +26,7 @@ pub async fn doc_close(host: &Host, window: &str, doc: DocId) -> IdeResult<()> { let project = host.project(window)?; blocking(move || { project.documents.close(doc); + ide_core::memory::release_free_pages_soon(); Ok(()) }) .await