perf(linux): trim glibc malloc arena after module loading#32662
Open
bartlomieju wants to merge 1 commit intomainfrom
Open
perf(linux): trim glibc malloc arena after module loading#32662bartlomieju wants to merge 1 commit intomainfrom
bartlomieju wants to merge 1 commit intomainfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
malloc_trim(0)on Linux after the main/side module loading phase completes, forcing glibc to release unused heap pages back to the OSmalloc_trimreclaims thisRoot cause
glibc's malloc doesn't automatically return freed memory to the OS — it keeps freed pages in its arena for future reuse. On Windows, the NT heap manager coalesces and releases freed memory much more aggressively. After TypeScript compilation/module loading (which creates many temporary allocations for AST nodes, source maps, etc.), glibc holds onto all that memory even though it's been freed.
Why this is safe
malloc_trimis a standard glibc function, safe to call at any time#[cfg(target_os = "linux")]— no-op on other platformsDENO_USR2_MEMORY_TRIMmechanism already uses the same call, just triggered externally via SIGUSR2Closes #25722
Test plan
cargo check -p deno)cargo test specs)Deno.memoryUsage().rssmalloc_trimcall is cfg-gated to Linux only)🤖 Generated with Claude Code