Skip to content

fix: add memory safety margin to prevent OOM before eviction - #61

Merged
kacy merged 1 commit into
mainfrom
fix/memory-safety-margin
Feb 8, 2026
Merged

fix: add memory safety margin to prevent OOM before eviction#61
kacy merged 1 commit into
mainfrom
fix/memory-safety-margin

Conversation

@kacy

@kacy kacy commented Feb 8, 2026

Copy link
Copy Markdown
Owner

summary

  • adds a 10% safety margin to memory limit enforcement so eviction triggers at 90% of the configured max_memory, leaving headroom for allocator overhead, fragmentation, and estimation error in per-entry constants
  • applies the margin in both Keyspace::enforce_memory_limit and ConcurrentKeyspace::set
  • surfaces max_memory_effective / max_memory_effective_human in INFO memory output so operators can see the actual enforcement threshold

what was tested

  • all 287 emberkv-core tests pass, including 4 new tests:
    • effective_limit_applies_margin — verifies 1000 → 900 at 90%
    • effective_limit_rounds_down — integer truncation behavior
    • effective_limit_zero — edge case
    • safety_margin_rejects_near_raw_limit — verifies writes that fit the raw limit but exceed the effective limit are correctly rejected
  • full workspace test suite passes (all crates)

design considerations

the alternative was integrating jemalloc-ctl for exact heap stats, but that adds a hard dependency on jemalloc and doesn't help on non-jemalloc builds. a percentage-based margin is allocator-agnostic, zero overhead, and easy to reason about. 90% was chosen as a conservative default — it trades ~10% of usable memory for a meaningful reduction in OOM risk under fragmentation.

the memory tracker uses hardcoded estimates (ENTRY_OVERHEAD = 96, etc.)
that can undercount actual allocator usage due to fragmentation and
per-allocation overhead. this means the process could exceed the
configured max_memory and get OOM-killed before eviction triggers.

applies a 10% safety margin so eviction kicks in at 90% of the
configured limit, leaving headroom for allocator overhead. the
effective limit is also surfaced in INFO memory output so operators
can see the actual enforcement threshold.
@kacy
kacy merged commit 1b449a4 into main Feb 8, 2026
5 checks passed
@kacy
kacy deleted the fix/memory-safety-margin branch February 8, 2026 18:43
kacy added a commit that referenced this pull request Feb 11, 2026
the memory tracker uses hardcoded estimates (ENTRY_OVERHEAD = 96, etc.)
that can undercount actual allocator usage due to fragmentation and
per-allocation overhead. this means the process could exceed the
configured max_memory and get OOM-killed before eviction triggers.

applies a 10% safety margin so eviction kicks in at 90% of the
configured limit, leaving headroom for allocator overhead. the
effective limit is also surfaced in INFO memory output so operators
can see the actual enforcement threshold.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant