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,
lowered to mcp_max_rss_mb — 512 MB — inside a stdio MCP server).
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. - adaptive_
batch_ size - Computes a memory-bounded work batch from current RSS and the guardian’s
hard threshold (1.5× the configured base limit). Falls back to
max_fileswhen RSS is unavailable. The lower bound keeps progress deterministic while allowing callers to reduce in-flight work substantially under low headroom. - allocator_
stats - Live heap bytes and allocator-resident bytes (
stats.allocated,stats.resident), when jemalloc is the allocator. The gap between process RSS andallocatedis what eviction cannot reach: allocator retention, code and thread stacks. - 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 memory in bytes, or
Noneif unavailable: resident set size on Linux and Windows, physical footprint on macOS (seemacos_footprint— resident size there hides compressed pages). - 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.
- reclaim_
now - Return freed pages to the OS now, then measure and publish the pressure again. For work that would otherwise refuse under pressure the guardian published earlier: memory a finished request dropped is not pressure.
- release_
system_ heap - Return glibc’s free pages to the OS. C dependencies other than tree-sitter
(SQLite, regex engines) still allocate through the platform malloc, whose
per-thread arenas otherwise keep every page an index build touched.
macOS has no equivalent:
malloc_zone_pressure_reliefreleased nothing in measurement, which is why tree-sitter is routed to jemalloc instead. - route_
tree_ sitter_ to_ jemalloc - Route tree-sitter’s C allocations (parse trees, parser stacks, query
cursors) through jemalloc, whose arenas
jemalloc_purgereturns to the OS. The platform malloc keeps them instead: measured on macOS, freed parser-sized blocks stayed in the footprint andmalloc_zone_pressure_reliefreleased nothing, so a daemon that parses every file of every root grew with each new root (#2075). - rss_
limit_ bytes - Returns the RSS limit in bytes: the
max_ram_percenttarget, lowered to the process cap when one is installed (set_process_rss_cap). - set_
process_ rss_ cap - Install an absolute RSS target for this process; the effective limit is the
lower of it and the
max_ram_percenttarget.0removes it. - 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). Eviction rounds that reclaim nothing back off to 60s, then pause for five minutes, at every pressure level; pressure logs repeat at most once a minute per level. The callback returns whether it reclaimed memory so the guard can distinguish a real retry from an empty-cache loop.