openclaw - 💡(How to fix) Fix Memory: Session files loaded entirely into memory via readFileSync

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

Impact

  • Gateway RSS grows linearly with session history
  • No pagination or streaming for session messages
  • sessions.json is a single 42MB file that gets parsed entirely on every access
  • Memory never shrinks because V8 GC is not aggressive enough
RAW_BUFFERClick to expand / collapse

Problem

OpenClaw gateway loads ALL session files into memory at startup:

  1. sessions.json (42MB in our case) is loaded via readFileSync + JSON.parse
  2. All JSONL session files (831 files, 389MB) are loaded via readFileSync per session access
  3. No lazy loading or streaming mechanism exists

This causes the gateway process to consume 3.5GB+ RSS on a 16GB machine after running for a few weeks.

Impact

  • Gateway RSS grows linearly with session history
  • No pagination or streaming for session messages
  • sessions.json is a single 42MB file that gets parsed entirely on every access
  • Memory never shrinks because V8 GC is not aggressive enough

Environment

  • OpenClaw version: latest
  • OS: macOS (ARM64)
  • Session count: 831 files
  • sessions.json size: 42MB

Suggested Fix

  1. Lazy-load session JSONL files only when needed
  2. Stream/paginate session messages instead of readFileSync
  3. Split sessions.json into per-session metadata files
  4. Add configurable memory limits
  5. Implement LRU cache for active sessions only

extent analysis

TL;DR

Implement lazy loading and streaming for session files to reduce memory consumption.

Guidance

  • Identify the most memory-intensive session files and prioritize lazy loading for those first.
  • Consider implementing a streaming mechanism for session messages to avoid loading entire files into memory at once.
  • Splitting the large sessions.json file into smaller, per-session metadata files could help reduce memory usage.
  • Evaluate the feasibility of implementing an LRU cache for active sessions to further optimize memory usage.

Example

// Example of lazy loading a session file
const fs = require('fs');
const sessionFilePath = 'path/to/session.jsonl';
fs.createReadStream(sessionFilePath, { encoding: 'utf8' })
  .on('data', (chunk) => {
    // Process the chunk of session data
  })
  .on('end', () => {
    // Session file has been fully loaded
  });

Notes

The provided suggestions assume that the OpenClaw gateway has the necessary infrastructure to support lazy loading and streaming. Additionally, the effectiveness of these suggestions may vary depending on the specific use case and requirements of the gateway.

Recommendation

Apply workaround: Implement lazy loading and streaming for session files, as this approach is likely to have the most significant impact on reducing memory consumption.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix Memory: Session files loaded entirely into memory via readFileSync