Skip to main content

Module process_memory

Module process_memory 

Source
Expand description

Keep resident memory close to what the process actually uses.

glibc keeps freed heap pages in its arenas, so after a burst (a long history loaded, a big request built, a turn finished) the resident set stays high even though the program no longer needs the memory. This module limits that and gives the pages back:

  • configure_system_allocator caps the number of malloc arenas and pins the mmap threshold, so large transient buffers are mmapped and returned to the OS as soon as they are freed (glibc otherwise raises the threshold dynamically and keeps them in the heap).
  • release_retained_heap runs malloc_trim(0).
  • RetentionWatch trims when memory that is resident but not allocated grows past the lowest level seen since the last trim.

Everything is a no-op outside Linux/glibc.

Structs§

RetentionWatch
Trims when retained memory grows threshold bytes past a baseline. The baseline is the level measured after the last trim and only moves down between trims: glibc’s free-chunk counters do not drop after a trim, so a plain threshold would fire again on every check.

Constants§

DEFAULT_ARENA_MAX
Default cap on glibc malloc arenas (glibc’s own default is 8 × cores).
DEFAULT_MMAP_THRESHOLD
Default mmap threshold: allocations at least this big bypass the arenas.

Functions§

configure_system_allocator
Apply the allocator limits once at startup. WHYCODES_GLIBC_ARENA_MAX and WHYCODES_GLIBC_MMAP_THRESHOLD override the defaults; 0 keeps glibc’s own behaviour for that setting.
parse_rss_anon
RssAnon: from a /proc/<pid>/status body, in bytes.
release_retained_heap
Return freed-but-retained heap pages to the OS.
release_retained_heap_debounced
release_retained_heap unless one already ran within min_interval. Returns whether it ran.
retained_bytes
Resident anonymous memory not held by live allocations: what a trim could give back. None where it cannot be measured.
rss_anon_bytes
RssAnon of this process in bytes (Linux).