Expand description
Process-level RAM guardian with adaptive eviction and hard OOM protection.
Monitors RSS via platform-specific APIs and triggers tiered cache eviction when memory usage exceeds configurable thresholds (default: 5% of system RAM). At critical levels, performs aggressive eviction and signals background tasks to abort. It never exits the process — recovery is always via eviction.
Structs§
- Memory
Snapshot - Snapshot of current memory state for diagnostics.
Enums§
Functions§
- abort_
requested - Returns
trueif the guardian has requested background tasks to abort. - current_
pressure - Returns the current pressure level as last observed by the guardian thread.
- force_
purge - Force immediate purge of all caches and jemalloc arenas.
- get_
rss_ bytes - Current process RSS in bytes, or
Noneif unavailable. - get_
rss_ bytes_ for_ pid - RSS of an arbitrary process by PID, or
Noneif unavailable/dead. - get_
system_ ram_ bytes - Total physical RAM in bytes, or
Noneif unavailable. - is_
under_ pressure - Quick, non-allocating memory pressure check for hot loops (scanners, indexers). Reads the cached atomic flag set by the guardian thread — O(1), no syscalls.
- jemalloc_
purge - Force-purge all jemalloc arenas to return memory to the OS.
Uses
MALLCTL_ARENAS_ALL(value 4096) which is the jemalloc sentinel for “all arenas”. Logs errors instead of silently swallowing them. - peak_
rss_ bytes - Recorded peak RSS since process start.
- rss_
limit_ bytes - Returns the RSS limit in bytes based on
max_ram_percentconfig. - start_
guard - Start the background memory guardian task (idempotent). Polls every 3s (normal), 1s (under pressure), or up to 15s once RSS has been stably calm (idle backoff). At Critical level, performs aggressive eviction and signals background tasks to abort — never exits the process.