fix: add memory safety margin to prevent OOM before eviction - #61
Merged
Conversation
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
summary
max_memory, leaving headroom for allocator overhead, fragmentation, and estimation error in per-entry constantsKeyspace::enforce_memory_limitandConcurrentKeyspace::setmax_memory_effective/max_memory_effective_humaninINFO memoryoutput so operators can see the actual enforcement thresholdwhat was tested
emberkv-coretests pass, including 4 new tests:effective_limit_applies_margin— verifies 1000 → 900 at 90%effective_limit_rounds_down— integer truncation behavioreffective_limit_zero— edge casesafety_margin_rejects_near_raw_limit— verifies writes that fit the raw limit but exceed the effective limit are correctly rejecteddesign considerations
the alternative was integrating
jemalloc-ctlfor 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.