Skip to content

perf: structuredClone in session store cache causes native memory leak (~1GB/min) #45438

@markus-lassfolk

Description

@markus-lassfolk

Problem

The gateway leaks ~1GB/min of native memory due to structuredClone usage in the session store cache. RSS grows to 4-5GB in minutes while V8 heap stays at ~1.2GB. The gap is native memory from structuredClone's serialization buffer that V8 GC cannot reclaim.

Evidence

  • Heap sampling profile: 242MB allocated by structuredClone in 5 seconds
  • RSS 5GB vs V8 heap 1.2GB = 3.8GB native memory leak
  • Crash trace: OOM at 4GB during after_compaction with 8467 facts in hybrid-memory store
  • Leak rate: ~1GB/min initially, gateway OOMs within 3-5 minutes

Root Cause

structuredClone in Node.js uses the structured clone algorithm which allocates native (C++) memory for the serialization buffer. For large session stores with frequent compaction cycles, these allocations accumulate faster than the GC can reclaim them.

Hot paths:

  1. src/config/sessions/store-cache.ts:60return structuredClone(cached.store) on every cache read
  2. src/config/sessions/store-cache.ts:71store: structuredClone(params.store) on every cache write
  3. src/config/sessions/store.ts:269return structuredClone(store) in loadSessionStore
  4. src/agents/auth-profiles/store.ts:28return structuredClone(store) in cloneAuthProfileStore

Suggested Fix

Replace structuredClone with JSON.parse(JSON.stringify()) or leverage the already-available serializedFromDisk / cached.serialized strings. Session stores are pure JSON data (no Maps, Sets, Dates, or circular refs), so JSON cloning is semantically equivalent but keeps all memory within V8's managed heap.

Environment

  • OpenClaw v2026.3.11
  • Node.js v25.6.0
  • 8467 facts in hybrid-memory SQLite store
  • Compaction runs every ~5 minutes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions