Skip to content

[RFC] Predictable and Safe MemoryCache Management for v6 #2647

@zalza13

Description

@zalza13

Is your feature request related to a problem?
Yes. In LiteDB v5 the MemoryCache can grow without clear limits (see issues #1756 and #2619).
This leads to unpredictable RAM usage in long-running applications.
Even if workloads are moderate the cache can retain too many free pages and slowly consume all available memory.

Describe the solution you'd like
Introduce a hardened MemoryCache design for v6 with:

  • Non-reentrant cleanup (prevent overlapping runs)
  • Atomic eviction flag on pages (avoid use-after-free)
  • On-demand cleanup trigger when free-list exceeds MaxFreePages
  • Safe disposal with cancellation
  • Conservative defaults (safe for production workloads)
  • Lightweight metrics (FreeCount, EvictedPages, etc.)
  • (Optional, opt-in) Byte caps and watermarks (MaxCacheSizeBytes, High/LowWatermarkBytes) for predictable memory ceilings

Describe alternatives you've considered

  • Keeping the current design: not ideal, memory growth remains unpredictable
  • Application-level workarounds (manual eviction, limiting usage): fragile and inconsistent
  • A complete redesign (e.g., full LRU): too complex and may break compatibility

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions