Unbounded graph growth & lack of observation GC: 850 MB monolithic JSON files (mem:graph:*) and 3.5GB+ heap OOM crash loops
Problem
Even with OpenTelemetry / iii-observability disabled (following up on #1389), agentmemory and iii-engine (v0.11.2) experience catastrophic in-memory allocation and crash-loops on long-running installations.
On a production workstation after ~2 months of routine agent usage, agentmemory.service was consuming 4.2 GB – 4.6 GB of memory + swap within 2 minutes of startup, repeatedly being killed by systemd-oomd / kernel cgroup memory controller (MemoryMax=4G), triggering continuous restart loops (restarting 33+ times in a single morning).
Root Cause Analysis
Forensic inspection of the underlying state store (state_store.db and stream_store) revealed three distinct architectural leaks:
1. Unbounded sourceObservationIds Accumulation in Knowledge Graph
Every time an observation is processed, entity extraction appends the observation ID to sourceObservationIds on matching nodes and edges without deduplication or truncation:
mem:graph:edges.bin: 447 MB on disk (82,237 edges, averaging 195 observation IDs per edge, max 4,964 IDs on a single edge).mem:graph:nodes.bin: 381 MB on disk (36,195 nodes).- Totaling over 16 million string ID references serialized inside these two keys.
2. Monolithic Single-Key In-Memory Graph Storage
agentmemory does not partition or index the graph in a relational or disk-backed format. The entire knowledge graph is stored as single key-value entries (mem:graph:nodes and mem:graph:edges).
During startup, retrieval (searchByEntities, expandFromChunks), snapshotting, and clustering, the code calls:
const allNodes = (await this.kv.list(KV.graphNodes)).filter((n) => !n.stale);
const allEdges = (await this.kv.list(KV.graphEdges)).filter((e) => !e.stale);In iii (Rust) and Node.js (V8), deserializing 850 MB of nested JSON with 16 million string objects expands into 3.5 GB to 4.5 GB of heap allocations.
3. Complete Lack of Retention / GC on Raw Observations and Streams
Even after observations are compressed by LLMs, synthesized into crystallized memories, and sessions are closed:
- Raw observation files (
state_store.db/mem:obs:*.bin) are never deleted or pruned. - Raw WebSocket stream event files (
stream_store/stream:mem-live:*.bin) are never cleaned up. - Over 2,851 raw observation files and 2,850 stream event files dating back months remained active in the file-based KV store. On boot,
iiiindexes every file in the directory into memory.
Impact & Reproduction
When a cgroup or system memory cap is enforced (e.g. MemoryMax=4G), the service cannot even complete initialization:
iiiboots and reads the directory index of ~3,000 files -> ~500 MB RSS.agentmemoryloadsKV.graphNodesandKV.graphEdges->iiiRSS jumps to 2.2 GB, Node RSS jumps to 1.5 GB+.- Cgroup limit of 4 GB is exceeded ->
systemd-oomd killed 2 process(es) in this unit,result: oom-kill. - Systemd restarts the service after 5s -> infinite crash loop.
Experimental Verification
To verify this root cause on the live instance:
- Archived
mem:graph:nodes.bin(381 MB) andmem:graph:edges.bin(447 MB). - Pruned 2,851 stale observation files (
mem:obs:*) and 2,850 stream files older than 3 days. - Restarted
agentmemory.service. - Result:
iiiprocess RSS immediately dropped from 2,162 MB to 459 MB (-78%).- Node process RSS dropped to 414 MB.
- Total service memory dropped from 4.6 GB to ~864 MB.
- All core memories (873 memories) and lessons (1,371 lessons) remained fully functional and queryable via
memory_recall. - The crash loop ceased completely.
Suggested Fixes
- Cap
sourceObservationIds: Limit the number of observation ID references retained on graph nodes and edges (e.g., ring buffer of latest 10–20 IDs), or store the edge-to-observation mapping in a separate inverted index rather than embedding unbounded arrays inside every edge object. - Implement TTL / Compaction for Raw Observations & Streams: Automatically prune or archive raw
mem:obs:*andstream:mem-live:*records once a session has been summarized and crystallized. - Partition / Index the Graph: Avoid
kv.list(KV.graphNodes)andkv.list(KV.graphEdges)loading the entire world into memory. Use indexed lookups by entity or prefix keys. - Relates to: #1389 (telemetry leak), #814 (O(n) kv.list timeouts), #1438 (observation deduplication).
Source: rohitg00/agentmemory