codebase-memory-mcp icon indicating copy to clipboard operation
codebase-memory-mcp copied to clipboard

Memory leak: process grows to 50+ GB virtual memory over hours/days, crashes Windows

Open baoyu0 opened this issue 5 months ago • 10 comments

Description

codebase-memory-mcp v0.8.1 has a severe memory leak when running as a persistent MCP server (stdio mode). Over the course of hours to ~2 days, the process grows to consume 50+ GB of virtual memory, eventually exceeding the system commit limit and crashing the entire Windows machine.

Environment

  • OS: Windows 10, 64-bit (build 10.0.26100)
  • codebase-memory-mcp version: v0.8.1 (installed via the official Windows .zip release, 269 MB binary)
  • RAM: 32 GB physical
  • Page file: system-managed
  • Agent: Hermes Agent (launches codebase-memory-mcp as a subprocess via MCP stdio config)
  • Single project indexed: C:/Users/zhaid/.pi/agent (~55k nodes, ~113k edges, ~97 MB on disk)
  • Config: auto_index=false, auto_index_limit=50000

Evidence

Windows Resource-Exhaustion-Detector (Event ID 2004) logged twice before the crash:

00:23:54 — 8 minutes before crash:

Process Virtual Memory (Commit Charge)
codebase-memory-mcp.exe (PID 52508) 46.4 GB
python.exe (Hermes companion) 59.4 GB

00:28:53 — 3 minutes before crash — still growing:

Process Virtual Memory
codebase-memory-mcp.exe (PID 52508) 54.5 GB (+8 GB in 5 min)
python.exe (Hermes companion) 59.3 GB

Combined commit of ~114 GB exceeded the system commit limit (~100 GB = 32 GB RAM + page file), triggering:

  • Volsnap event 6: "The shadow copy of volume C: could not create a new page stack. The system may be low on virtual memory."
  • OmenCommandCenterBackground.exe crashed with System.OutOfMemoryException
  • Everything.exe crashed with stack overflow (0xc00000fd)
  • dwm.exe (Desktop Window Manager) crashed 3 times
  • AUDIODG.EXE (audio) crashed
  • Eventually Kernel-Power 41: system rebooted uncleanly

Recurrence

This has happened at least 3 times since installation (June 12):

  • Jun 13: same pattern — the process consumed ~107 GB
  • Jun 15: ~99 GB
  • Jun 23: ~114 GB (the crash described above)

Comparison vs normal state

Right now after a fresh restart:

codebase-memory-mcp.exe   PID=22416  WS=12.3 MB

Working set is 12 MB. The commit charge grows to 50+ GB over time, which is clearly a memory leak in a long-running data structure.

Suspected cause

The knowledge graph data structure (nodes, edges, PageRank scores, etc.) is being retained in process memory without eviction as the index accumulates queries and incremental updates. The on-disk index is only ~97 MB, indicating the in-memory representation is leaking by multiple orders of magnitude.

Steps to reproduce

  1. Run codebase-memory-mcp in stdio MCP server mode (as Hermes/Claude Code Agent would)
  2. Let it run for several hours to 2 days while the agent queries it
  3. Monitor virtual memory usage — it will climb steadily and never decrease
  4. Eventually the system runs out of commit charge and crashes

Note: auto_index=false does not prevent the leak — the growth happens on query/read operations, not just indexing.

Request

Please investigate the in-memory graph cache and query result retention. Consider:

  • Adding an LRU eviction policy for cached query results
  • Streaming graph data instead of keeping the full index in RAM
  • A configurable max-memory limit that triggers compaction/eviction

baoyu0 avatar Jun 23 '26 15:06 baoyu0

Hey, thx for noticing. It's probably a gap in our soak test suite. Thx also for the contribution. Will check

DeusData avatar Jun 23 '26 20:06 DeusData

This issue now has a first-class, durable reproduction in the cumulative bug-reproduction suite landing in #667 — a test that pins the exact failing behaviour, so it's tracked and guarded against silent regression. Where #667 also lands the root-cause fix, that's called out in the PR; otherwise the reproduction makes the eventual fix straightforward and verifiable.

The reproduction goes live once #667 merges. (Posted from the release branch's QA pass.)

DeusData avatar Jun 28 '26 14:06 DeusData

Adding a Linux data point — same leak, same version, different OS:

Environment: v0.8.1 (official Linux x86-64 binary), Ubuntu 24.04 in an LXC container, stdio MCP server under Claude Code, one instance per CC session, auto_index=false, single indexed repo (~190K LOC, ~48MB on-disk index).

Observed growth (RSS, per instance): ~10-250MB fresh → 1.15-1.35GB after ~1 day → 2.4-2.5GB after ~1.5 days → ~4GB shortly after under continued query load. Confirms your note that auto_index=false does not prevent it — growth tracks query/read operations.

Compounding failure mode worth flagging: because each agent session spawns its own instance, concurrent sessions multiply the leak. We hit a hard host OOM with 8 instances (~14.5GB combined), and later multi-minute memory-pressure freezes with just 2 instances (~8GB combined) on a 16GB container.

Interim containment that works well on Linux, in case it helps others: launch the server inside a transient systemd scope — systemd-run --user --scope -p MemoryMax=2G -- codebase-memory-mcp. The kernel kills the instance at the cap, the agent session degrades gracefully, and a reconnect gets a fresh process. Wrapper implementation here if useful: https://github.com/WingedGuardian/GENesis-AGI/pull/902

Happy to provide RSS-over-time logs or test a candidate fix.

WingedGuardian avatar Jul 05 '26 19:07 WingedGuardian

Thanks for the report! To move this forward we need a bit more so we can reproduce it ourselves:

  • the codebase-memory-mcp --version you're on
  • the exact steps or command you ran
  • a public repo (or a small dummy snippet) that shows the problem — please don't paste proprietary code

Once that's here we'll pick it straight back up. Heads-up: issues left awaiting-reporter are automatically closed after a few weeks of silence, but a comment reopens the door anytime.

github-actions[bot] avatar Jul 08 '26 12:07 github-actions[bot]

v0.9.0 includes memory-retention fixes, bounded retention work, and better peak/live RSS accounting. Could you retest this Windows memory case on v0.9.0 and share current peak/live RSS if it still grows heavily? Marking awaiting-reporter.

DeusData avatar Jul 08 '26 12:07 DeusData

Retest on v0.9.0 — Windows 10, RTX 5060 Ti

Quick functional + memory test:

Step RSS Delta
Fresh start 14.2 MB —
After tools/list (8 tools) 14.3 MB +120 KB
After search_graph (hermes, "config") 15.0 MB +628 KB
After get_architecture (hermes) 15.0 MB +20 KB
After 5 more search_graph calls 15.0 MB +8 KB

Total growth across 13 operations: ~776 KB. No immediate ballooning.

Caveat: This is a short (~2 minute) test. The original issue manifested over hours/days. Short-term RSS looks normal, but I can't confirm the sustained-growth fix without an extended soak.

Could you share what specific changes in v0.9.0 address the retention pattern? If there's a specific codepath (index reload, query accumulation, etc.) you'd like me to hammer on, happy to run a longer targeted test.

baoyu0 avatar Jul 09 '26 05:07 baoyu0

Thanks for the retest and the offer — a targeted soak is exactly what this needs, and here's the honest picture.

What changed in v0.9.0 that's relevant to long-run retention:

  1. Supervised-subprocess isolation for background auto-index/watcher work — the most relevant change for an hours/days pattern. Re-index cycles triggered by file changes now run in a supervised child process, so their memory is returned to the OS when each cycle ends instead of accumulating in the long-lived server process. Our leading suspect for the original 50+ GB growth on Windows was exactly this: continuous watcher-triggered re-index churn (see #841) retaining allocator pages in the parent across hundreds of cycles.
  2. Bounded, source-capped parallel retention during indexing (caps peak RSS on each cycle).
  3. A cross-thread slab-free fix (aligned-page use-after-free — could also corrupt/retain in long-lived processes).

To be explicit about what we have NOT proven: our own long-run reproduction for the sustained-growth pattern is still open — short-run RSS looking flat (which we also see, matching your table) never proved this fixed, which is why the issue stays open until a real soak says so.

The soak that would actually hammer the suspected codepath:

  • Keep the MCP server running with auto-sync active on an indexed repo.
  • Generate steady file-change churn — e.g. a loop that touches/edits a handful of source files every 1–2 minutes (an active working day compressed).
  • Sample the server process RSS every ~15 minutes for 4+ hours (Get-Process | Select WS or Task Manager).
  • v0.8.1 behavior: staircase growth that never comes back down. Expected v0.9.0 behavior: sawtooth per cycle, flat baseline.

Query traffic (search_graph etc.) is a much less likely driver — your 13-op test already suggests that path is clean. If the watcher soak shows a flat baseline over hours, we'll close this; if it still staircases, an RSS-over-time series plus the repo's rough size is everything we'd need to dig back in.

DeusData avatar Jul 09 '26 21:07 DeusData

Thank you again for the retests and for distinguishing short-run RSS from the original hours-long failure. The later release discussion #1144 records additional Windows allocator/thread-lifecycle work beyond the July hypothesis here. This sweep does not establish a fresh long-run result for your setup, and I am not asking you to risk another machine-wide OOM; the remaining confirmation belongs with controlled maintainer testing.

DeusData avatar Oct 19 '26 22:10 DeusData

Thank you for your patience. Sorry for the delay. Long-run Windows memory confirmation still belongs in controlled maintainer testing; please do not risk another machine-wide OOM. I’ll follow up soon.

DeusData avatar Oct 25 '26 17:10 DeusData

Understood — I will not run another machine-wide soak; the v0.9.0 short-run RSS numbers I posted are all I can offer safely from this machine. Standing by for the maintainer-side long-run result, no action needed from me.

baoyu0 avatar Nov 19 '26 14:11 baoyu0