Skip to main content

Module heap_trim

Module heap_trim 

Source
Expand description

Handing freed heap memory back to the OS on long runs.

libxml2/libxslt allocate on glibc’s heap, and glibc keeps freed mid-heap pages mapped: a book-scale core pass or split render cycles a DOM per fragment or page through it, so RSS creeps into the memory fuse unless the free pages are trimmed (the 131 MB witness: core RSS 22→36 GB, post +152 KB/page). A trim walks the whole heap and madvises EVERY free chunk again, so a trim per fixed page count costs pages × heap: 203 K madvise calls at 8,094 pages on Nasser’s document (#777 FAQ, 130 of 194 min in sys under WSL 1). sample trims only when the resident set grew by step_kb since the last trim, which bounds the trims by the run’s memory growth instead of its length.

Constants§

SAMPLE_PAGES
The page cadence of sample in the render loops: one /proc/self/status read per this many pages.

Functions§

c_heap_mb
glibc’s heap in MB, (live, freed but retained): mallinfo2’s uordblks and fordblks, all arenas. libxml2 and libxslt allocate there, Rust through mimalloc; diagnostic only.
collect_rust_heap
Purge mimalloc’s free pages (skipped under dhat-heap).
release_free_heap
Release the free pages of both heaps: glibc’s (malloc_trim) and mimalloc’s (collect_rust_heap).
sample
Sample the resident set and trim when it grew by step_kb since the last trim. Returns whether it trimmed. Callers sample at a page or fragment cadence; the first sample of a thread trims.
step_kb
The resident growth that warrants a trim: 1/64 of the soft memory fuse (resolve_rss_cap, 3/4 of --max-memory), within 32-256 MB: 96 MB under --max-memory=8192, the 32 MB floor below a ceiling of ~2.7 GB, ~67 MB at the 4.5 GB default fuse, 256 MB with none. Per sample this replaces up to step/page-cost of retained free pages (~650 pages at the 152 KB/page growth) with one /proc/self/status read. A render-worker child (render_workers::worker_main) installs its manifest.max_rss_kb share via stomach::set_memory_cap, so it steps by its own share (or by the default fuse / LATEXML_RSS_CAP_BYTES when uncapped).