Fix dirty memory in order: measure, de-allocate the hot path, bound every edge, cap sizes — tune the allocator last

Asked (summary)

What is the best way to reduce dirty memory in a Rust runtime — hot-path allocation, unbounded queues, and so on?

Built from 12 published recommendations across 4 independent hosts (docs.rs, microsoft.github.io, swe.servos.dev, mucahitkurtlar.com). Several rows repeat the same bounded-channel and mimalloc purge-delay advice from different pages; the narrative consolidates them, and specialized allocators are treated as options to benchmark, not defaults.

Priority flow: diagnosis to remediation

Hover a stage for concrete actions. Blue stages fix live memory; the orange stage only treats freed pages the allocator retains.

Hot paths should be allocation-free: pass references or Cow instead of cloning inside loops, and measure every change with criterion before committing it — docs.rs
With finite capacities on every retaining edge, retained memory becomes reviewable arithmetic (capacity × item bytes) instead of being limited only by available RAM — but the payload budget excludes allocator, task and TLS overhead — swe.servos.dev
A reused arena with reset() stabilizes at one warm chunk and makes no system calls per round — but skip the reset and it grows forever — docs.rs
Dropping mimalloc's purge_delay from v3's 1000 ms to 10 ms cut post-drop RSS on a 2 GB heap from 2006 MB to 156 MB — yet the settled floor barely moved, because it was live memory, not allocator-held — docs.rs

The memory-budget formula

retained payload ≈ queue capacity × max item bytes  +  in-flight concurrency × per-task working-set bytes

Allocator fragmentation, task/future and channel bookkeeping, TLS state, socket buffers and kernel overhead sit on top of this and must be budgeted separately — a payload-only figure does not predict RSS exactly. The point is that once every retaining edge is finite, this number exists at all and is reviewable in code review.

The key diagnostic split

Live heap stays high

Ownership and retention problem

Find who owns the bytes: unbounded queues, retry lists, fan-out, caches, clones kept alive. Allocator tuning cannot help — stages 2–4 are the fix.

Live heap drops, RSS / Private_Dirty stays high

Allocator caching or fragmentation

The allocator retains freed pages. Enable jemalloc's background_thread with a dirty decay, or lower mimalloc's purge delay — set early, before pre-main allocation latches it. Over-aggressive purging trades OOM risk for madvise churn on the hot path.

Decision table: all 12 recommendations

TechniqueActionTradeoffSource

Source: 12 technique recommendations (action, benefit, tradeoff) gathered from 4 independent hosts; one row per source page. Numbers such as RSS in MB and purge delays in ms are the figures reported by each source, not re-measured here. Actions and tradeoffs are truncated in the table for space; benefits are summarized in the narrative and findings.

This report was generated automatically by Keenable SELECT at a user's request, from publicly available web sources linked herein. Keenable does not review, verify, or endorse its contents and makes no representation as to accuracy, completeness, or timeliness; AI-based extraction may contain errors. Nothing in this report is investment, legal, financial, or other professional advice. All trademarks and referenced content remain the property of their respective owners; no affiliation or endorsement is implied. To report an error, rights concern, or request removal: legal@keenable.ai.

Keenable SELECTAsk your own question
Made with Keenable SELECT