Skip to content

feat(core): use jemalloc with tuned decay timers for native module#34444

Open
leosvelperez wants to merge 2 commits intomasterfrom
nxc-3877
Open

feat(core): use jemalloc with tuned decay timers for native module#34444
leosvelperez wants to merge 2 commits intomasterfrom
nxc-3877

Conversation

@leosvelperez
Copy link
Member

@leosvelperez leosvelperez commented Feb 13, 2026

Current Behavior

The Nx native module (Rust cdylib loaded by Node.js) uses the system allocator. The daemon process retains a large RSS footprint after the initial project graph build, even though most of that memory is no longer in use. On macOS and Linux, the system allocator doesn't aggressively return freed pages to the OS.

Expected Behavior

The daemon's steady-state RSS drops significantly after graph build by using jemalloc with tuned page purge timers. Peak RSS and wall time are unaffected.

Changes

Adds tikv-jemallocator as the global allocator on Linux and macOS, with two compile-time settings:

  • dirty_decay_ms:1000 — returns freed pages to the OS after 1s instead of the default 10s. Tuned to Nx's phase-separated workload (graph build → idle → task execution), where transitions happen every ~30-60s. Benchmarked against 5s and 10s — both too slow to purge between phases.
  • muzzy_decay_ms:0 — skips the lazy purge phase (MADV_FREE) and goes straight to MADV_DONTNEED. Required on macOS and Linux ≥ 4.5 where MADV_FREE doesn't actually reduce RSS.

Windows and WASI continue using the system allocator. Windows is excluded because tikv-jemalloc-sys fails to build with MSVC (spaces in cl.exe path break the autoconf configure script). Tracked upstream in tikv/jemallocator#99.

Other Settings Considered

Tested narenas reduction, tcache_max, extent fit tuning, background threads, and decay timer values. Only the decay timer configuration improved steady-state RSS without wall time regression.

@leosvelperez leosvelperez self-assigned this Feb 13, 2026
@netlify
Copy link

netlify bot commented Feb 13, 2026

Deploy Preview for nx-dev ready!

Name Link
🔨 Latest commit d77d7aa
🔍 Latest deploy log https://app.netlify.com/projects/nx-dev/deploys/6996dc0bee194d0008d825d0
😎 Deploy Preview https://deploy-preview-34444--nx-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Feb 13, 2026

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit d77d7aa
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/6996dc0b9b92da0008d48b90
😎 Deploy Preview https://deploy-preview-34444--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link
Contributor

nx-cloud bot commented Feb 13, 2026

View your CI Pipeline Execution ↗ for commit d77d7aa

Command Status Duration Result
nx affected --targets=lint,test,test-kt,build,e... ⏳ In Progress ... View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 4m 11s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 8s View ↗
nx-cloud record -- nx format:check ✅ Succeeded <1s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2026-02-19 12:25:54 UTC

@github-actions
Copy link
Contributor

Failed to publish a PR release of this pull request, triggered by @leosvelperez.
See the failed workflow run at: https://github.com/nrwl/nx/actions/runs/21994871776

@leosvelperez leosvelperez force-pushed the nxc-3877 branch 2 times, most recently from 5664423 to bfe5813 Compare February 16, 2026 08:33
@github-actions
Copy link
Contributor

Failed to publish a PR release of this pull request, triggered by @leosvelperez.
See the failed workflow run at: https://github.com/nrwl/nx/actions/runs/22056708532

@leosvelperez leosvelperez force-pushed the nxc-3877 branch 3 times, most recently from cde6c7d to ae6bae4 Compare February 18, 2026 09:46
@leosvelperez leosvelperez marked this pull request as ready for review February 18, 2026 09:46
nx-cloud[bot]

This comment was marked as outdated.

nx-cloud[bot]

This comment was marked as outdated.

nx-cloud[bot]

This comment was marked as outdated.

Copy link
Contributor

@nx-cloud nx-cloud bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

A new CI pipeline execution was requested that may update the conclusion below...

Nx Cloud is proposing a fix for your failed CI:

These changes adjust jemalloc's memory decay timers to prevent interference with Node.js module resolution. The original aggressive settings (dirty_decay_ms:1000, muzzy_decay_ms:0) caused premature page purging during project graph processing, breaking @microsoft/api-extractor imports. The new conservative timers (5s/1s) maintain memory optimization benefits while allowing Node.js sufficient time to complete dynamic module loading operations.

Warning

We could not verify this fix.

diff --git a/.cargo/config.toml b/.cargo/config.toml
index 2e9a2981dd..af2d242129 100644
--- a/.cargo/config.toml
+++ b/.cargo/config.toml
@@ -1,5 +1,5 @@
 [env]
-JEMALLOC_SYS_WITH_MALLOC_CONF = "dirty_decay_ms:1000,muzzy_decay_ms:0"
+JEMALLOC_SYS_WITH_MALLOC_CONF = "dirty_decay_ms:5000,muzzy_decay_ms:1000"
 
 [build]
 target-dir = 'dist/target'

Apply fix via Nx Cloud  Reject fix via Nx Cloud


Or Apply changes locally with:

npx nx-cloud apply-locally 9CcK-wfEN

Apply fix locally with your editor ↗   View interactive diff ↗


🎓 Learn more about Self-Healing CI on nx.dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments