Skip to content

fix: return freed native memory to the operating system on EvictCache with glibc - #64

Open
dev-miko wants to merge 1 commit into
evolvedlight:developfrom
swisspost:fix/trim-native-memory
Open

dev-miko wants to merge 1 commit into
evolvedlight:developfrom
swisspost:fix/trim-native-memory

Conversation

@dev-miko

@dev-miko dev-miko commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Problem

On Linux with glibc, EvictCache() frees the compilation cache, but the process keeps the memory. glibc keeps freed memory in its heaps for later allocations, so a process stays at the peak of its largest compilation even after clearing the cache. Under a container memory limit, that is no better than a leak.

Change

  • EvictCache() and EvictCache(0) call malloc_trim(0) after comemo::evict(0), on target_os = "linux", target_env = "gnu" only (linux-x64, linux-arm64).
  • EvictCache(maxAge) with maxAge > 0 does not trim, so that regular partial evictions do not trim the heaps every time.
  • libc is a new dependency for that target only. It was already in Cargo.lock through other crates. There is no Cargo feature and no new allocator.
  • New test tests/memory_release.rs (Linux with glibc only). It fills the cache on a worker thread, then clears it once from a thread that compiled and once from a new thread that never did.
  • README section and release note. Both also correct the existing claim that the packaged Linux library uses musl.

Measurements

Setup: two 1000-page synthetic documents, compiled at once on thread-pool threads with one reused TypstCompiler each and new sys.inputs every round. EvictCache() is called from the main thread after each round. .NET 11.0.0, glibc 2.43 (mcr.microsoft.com/dotnet/sdk:11.0), Docker Desktop with 2 CPUs and an 8 GB limit, WSL2 kernel 6.18, THP madvise, x64. Every run is a fresh process. RSS is VmRSS, and the peak is VmHWM, reset before each round.

develop this PR
Peak, round 1 1.33–1.36 GB 1.33–1.37 GB
Peak, round 6 / round 15 1.40–1.45 GB / 1.46 GB 1.39 GB / 1.43 GB
Held after EvictCache(), round 1 1.31–1.36 GB 0.50–0.52 GB
Held after EvictCache(), round 6 1.40–1.45 GB 0.82–0.83 GB
Held after EvictCache(), round 15 1.46 GB 0.92 GB
EvictCache() duration 256–375 ms 368–723 ms
malloc_trim(0) alone – 110–135 ms
Max latency of a one-page compilation on another thread that overlaps EvictCache() 297–394 ms 308–355 ms
  • Without the trim, the process holds what it peaked at. With it, what it holds after EvictCache() still grows across rounds (0.52, 0.64, 0.71, 0.77, 0.81 GB …) and levels off at about 0.9 GB from round 10. The trim returns whole free pages only and does not defragment.
  • The overlapping one-page compilation is dominated by the eviction itself in both builds. With the trim, 6 such compilations overlapped the EvictCache() window, against 1 without it.
  • MALLOC_ARENA_MAX=2 made no difference: develop held 1.28–1.38 GB, and this PR held 0.52 GB in round 1 and 0.89 GB in round 6.
  • tests/memory_release.rs fails with the trim commented out: the compilation added 79 MB, and 42.6 MB stayed resident after clearing the cache. With the trim, 4.3 MB and 2.5 MB stay, out of 81 MB and 78 MB.

Limits

  • glibc only. musl, Windows and macOS are unchanged and were not measured.
  • The trim covers the native heap of the whole process, including the host's own native allocations and the vendored OpenSSL. It locks one glibc arena at a time, so an allocation that needs the arena being trimmed waits.
  • It does not lower the peak of a single compilation. To bound that, split large documents.
  • The comemo cache keeps the capacity of its hash tables after evict(0). That capacity grows with the largest document compiled, and the trim cannot return it.

Not tested

  • Not measured on linux-arm64 or with THP always. Not built locally for musl. The package tests of this PR cover the other runtime IDs.

… with glibc

On Linux with glibc, EvictCache() freed the cache but the process kept the
memory: glibc keeps freed memory in its heaps for later allocations, so a
process stayed at the peak of its largest compilation after clearing the cache.

EvictCache() and EvictCache(0) now call malloc_trim(0) after evicting, on
Linux with glibc only. It trims the heaps of the whole process, not only of the
library. EvictCache(maxAge) with maxAge > 0 does not trim, and other targets
are unchanged.

Measured with two 1000-page documents compiled at once on thread-pool threads
and EvictCache() called from the main thread after each round, in a .NET 11.0.0
process on glibc 2.43 (Docker, 2 CPUs, WSL2 kernel 6.18):

- held after the first EvictCache(): 1.31 GB before, 0.50 GB after;
- held after round 15: 1.46 GB before, 0.92 GB after (leveled off at about
  0.9 GB from round 10, because the trim does not defragment);
- peak: 1.33 to 1.46 GB before, 1.33 to 1.44 GB after;
- malloc_trim(0) took 110 to 135 ms;
- a one-page compilation on another thread that overlapped EvictCache() took
  at most 297 to 394 ms before and 308 to 355 ms after, dominated by the
  eviction itself.

The new test memory_release.rs fails without the trim (79 MB added by the
compilation, 42.6 MB still resident after clearing the cache) and passes with
it, both when the cache is cleared from a thread that compiled and from a new
thread that never did.

Also corrects the README and the EvictCache() release note, which said the
packaged Linux library uses musl.
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