feat: Memory optimization to prevent OOM in multi-session scenarios#1437
Open
oyi77 wants to merge 1 commit into
Open
feat: Memory optimization to prevent OOM in multi-session scenarios#1437oyi77 wants to merge 1 commit into
oyi77 wants to merge 1 commit into
Conversation
Add memory pressure monitoring, conversation compaction triggers, and resource lifecycle management to prevent Out of Memory errors when running multiple OpenClaude sessions concurrently. Changes: - Add memory pressure monitor with elevated/critical thresholds (src/utils/memoryPressure.ts) - Add memory compaction trigger that responds to pressure levels (src/utils/memoryCompaction.ts) - Wire paste store cleanup into background housekeeping (src/utils/backgroundHousekeeping.ts) - Add OPENCLAUDE_MAX_ACTIVE_MESSAGES conversation limit (default: 200) (src/QueryEngine.ts) - Add --max-memory CLI flag for per-session heap limit (bin/openclaude) - Add concurrent session budget auto-scaling (src/utils/concurrentSessions.ts) - Start memory pressure monitor on initialization (src/main.tsx) Environment variables: - OPENCLAUDE_MAX_MEMORY_MB: per-session memory budget (default: 1536) - OPENCLAUDE_MAX_ACTIVE_MESSAGES: max messages before compaction (default: 200) - --max-memory=MB: CLI flag for V8 heap limit Fixes OOM when running 4+ sessions on 32GB systems. Each session now stays bounded with proactive compaction when memory pressure rises.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses Out of Memory (OOM) errors when running multiple OpenClaude sessions concurrently. On a 32GB system, 4 instances can exhaust all available RAM. This PR adds memory-pressure-aware conversation compaction, automatic paste store cleanup, configurable per-session memory budgets, and concurrent session budget scaling.
Changes
New Files
src/utils/memoryPressure.ts— Memory pressure monitor that watches RSS every 30s with two thresholds:elevated(80% of budget): triggers conversation compactioncritical(90% of budget): forces aggressive compaction + cache pruningsrc/utils/memoryCompaction.ts— Connects pressure levels to compaction actionsModified Files
src/utils/backgroundHousekeeping.ts— WiredcleanupOldPastes()into background housekeeping (previously existed but was never auto-triggered)src/main.tsx— Start memory pressure monitor on initializationsrc/QueryEngine.ts— AddedOPENCLAUDE_MAX_ACTIVE_MESSAGEScheck (default: 200) to trigger compaction when message count exceeds limitbin/openclaude— Added--max-memory=MBCLI flag that maps to existing V8 heap limitsrc/utils/concurrentSessions.ts— AddedcalculatePerSessionMemoryBudget()that auto-adjusts based on concurrent session countEnvironment Variables
OPENCLAUDE_MAX_MEMORY_MBOPENCLAUDE_MAX_ACTIVE_MESSAGES--max-memory=MBKey Findings
During analysis, several existing mechanisms were discovered that complement this work:
--max-old-space-size(default 8GB)The main gaps were: no memory pressure detection, paste cleanup never called automatically, no message count limits, and no concurrent session budget scaling.
Testing
bun run buildpassesHow It Works
memoryPressuremonitor begins watching RSS every 30selevatedpressureRelated