Skip to content

[Bug] Gateway memory leak: sessions.json loaded entirely into RAM, grows unbounded #51097

@guruk

Description

@guruk

Bug: Gateway memory leak — sessions.json loaded entirely into RAM, grows unbounded

Version: 2026.3.13
Platform: macOS (Darwin arm64, Apple Silicon)
Uptime at time of report: 19–31 days

Summary

The openclaw-gateway process exhibits a significant memory leak under long-running conditions. After ~19 days of continuous operation with active cron jobs, gateway RAM usage reaches 1.3 GB+, causing slow responses and eventual unresponsiveness.

Root Cause (identified)

The file ~/.openclaw/agents/main/sessions/sessions.json grows unbounded — on our deployment it reached 150,000 lines / 7.4 MB on disk after ~3 weeks. The gateway appears to load this file entirely into RAM and keep it in memory, growing with every conversation turn.

Evidence:

RSS: 1338 MB after 19 days uptime
sessions.json: 7.4 MB / 150,179 lines
~/.openclaw/agents/main/sessions/: 134 MB total

The machine has multiple agents running 25+ cron jobs daily, each producing isolated sessions — the session history accumulates very rapidly.

Impact

  • Gateway becomes sluggish and eventually stops responding to channel messages (Telegram/WhatsApp)
  • Only fix currently: restart the gateway process
  • Affects any long-running deployment with active cron jobs

Suggested Fixes

  1. Trim sessions.json — keep only the last N sessions (e.g. 500) per agent, archive or drop older ones
  2. Lazy load session history — do not load all session history into RAM at startup; load on demand per session
  3. Streaming/append-only writes — avoid rewriting the entire sessions.json on every turn
  4. Expose a cleanup command — e.g. openclaw sessions prune --older-than 7d

Workaround

Daily gateway restart via launchd cron when RSS exceeds threshold:

RSS=$(ps -o rss= -p $(pgrep -f openclaw-gateway))
if [ "$RSS" -gt 700000 ]; then
  launchctl bootout gui/$(id -u) ~/Library/LaunchAgents/ai.openclaw.gateway.plist
  sleep 3
  launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/ai.openclaw.gateway.plist
fi

This works but is clearly a bandaid. The underlying issue needs to be addressed in the gateway session management code.

Additional context

  • Two separate machines (.52 Mac Mini, .53 MacBook Pro) both exhibit identical behavior
  • Senior machine (.53): 939 MB after 31 days
  • Mini machine (.52): 1338 MB after 19 days (more crons = faster growth)
  • Both machines: KeepAlive=true in plist, so gateway auto-restarts after kill

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