Repository navigation
Conversation
… 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.
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.
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()andEvictCache(0)callmalloc_trim(0)aftercomemo::evict(0), ontarget_os = "linux",target_env = "gnu"only (linux-x64,linux-arm64).EvictCache(maxAge)withmaxAge > 0does not trim, so that regular partial evictions do not trim the heaps every time.libcis a new dependency for that target only. It was already inCargo.lockthrough other crates. There is no Cargo feature and no new allocator.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.Measurements
Setup: two 1000-page synthetic documents, compiled at once on thread-pool threads with one reused
TypstCompilereach and newsys.inputsevery 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, THPmadvise, x64. Every run is a fresh process. RSS isVmRSS, and the peak isVmHWM, reset before each round.developEvictCache(), round 1EvictCache(), round 6EvictCache(), round 15EvictCache()durationmalloc_trim(0)aloneEvictCache()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.EvictCache()window, against 1 without it.MALLOC_ARENA_MAX=2made no difference:developheld 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.rsfails 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
evict(0). That capacity grows with the largest document compiled, and the trim cannot return it.Not tested
linux-arm64or with THPalways. Not built locally for musl. The package tests of this PR cover the other runtime IDs.