Skip to content

Commit 6340591

Browse files
bartlomiejuclaude
andcommitted
perf(linux): trim glibc malloc arena after module loading
On Linux, glibc's malloc doesn't release freed memory back to the OS, causing RSS to be 3-5x higher than on Windows for the same workload. This is especially noticeable when loading large TypeScript codebases (e.g. generated OpenAPI clients) where module compilation creates heavy allocation churn. Call `malloc_trim(0)` after the main/side module loading phase completes to force glibc to release unused heap pages back to the OS. Closes #25722 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 40b56f7 commit 6340591

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

cli/lib/worker.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,12 +855,36 @@ impl LibMainWorker {
855855

856856
pub async fn execute_main_module(&mut self) -> Result<(), CoreError> {
857857
let id = self.worker.preload_main_module(&self.main_module).await?;
858-
self.worker.evaluate_module(id).await
858+
self.worker.evaluate_module(id).await?;
859+
860+
// After loading and evaluating all modules, trim the glibc malloc arena.
861+
// Module loading/TypeScript compilation creates heavy allocation churn
862+
// that glibc's allocator doesn't release back to the OS, causing RSS on
863+
// Linux to be much higher than on other platforms (see #25722).
864+
#[cfg(target_os = "linux")]
865+
{
866+
// SAFETY: calling libc malloc_trim which is safe to call at any time.
867+
unsafe {
868+
libc::malloc_trim(0);
869+
}
870+
}
871+
872+
Ok(())
859873
}
860874

861875
pub async fn execute_side_module(&mut self) -> Result<(), CoreError> {
862876
let id = self.worker.preload_side_module(&self.main_module).await?;
863-
self.worker.evaluate_module(id).await
877+
self.worker.evaluate_module(id).await?;
878+
879+
#[cfg(target_os = "linux")]
880+
{
881+
// SAFETY: calling libc malloc_trim which is safe to call at any time.
882+
unsafe {
883+
libc::malloc_trim(0);
884+
}
885+
}
886+
887+
Ok(())
864888
}
865889

866890
pub async fn execute_preload_modules(&mut self) -> Result<(), CoreError> {

0 commit comments

Comments
 (0)