[BUG] Memory leak and runaway write loop in `claude --continue` causes 28GB memory usage
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Environment
- Claude Code Version: 2.0.28
- OS: macOS 14.7.5 (23H527)
- RAM: 36 GB
- Install Method: native
## Description
When running claude --continue to resume a previous session, the process entered an infinite
loop continuously writing the session state file, consuming 27.87 GB of memory and causing
severe system memory pressure (13.11 GB swap used).
## Symptoms
- Process PID 42914 running for 2+ hours with high CPU (382.8%)
- Memory consumption: 27.87 GB (actual RAM, not virtual)
- System memory pressure: 32.73/36 GB used + 13.11 GB swap
- Debug log showed 74,209 identical write cycles in ~2 hours
## Root Cause Analysis
### Debug Log Evidence (~/.claude/debug/43bcb8fd-83ca-4341-8f9f-22715b34697c.txt)
- 71 MB debug file with 1,113,361 lines
- 74,209 writes of
.claude.json(16 MB each) = ~1.2 TB disk I/O - 296,808 hook checks all returning 0 results
- 148,388 file history snapshots
Most common debug patterns:
296808 [DEBUG] Hooks
148388 [DEBUG] FileHistory
74226 [DEBUG] Found 0 hook matchers in settings
74213 [DEBUG] Writing to temp file
74212 [DEBUG] Temp file written successfully, size
74209 [DEBUG] File /Users/tonymilton/.claude.json written atomically
### Session State File Issue
The .claude.json file was 16 MB due to:
- 52 history items with large pasted images (base64-encoded)
- Largest single item: 1.1 MB
- Total of 15.78 MB in pasted content
### Runaway Loop Pattern
The --continue flag appears to have triggered a loop where the process continuously:
- Checked for hooks (finding none)
- Created file history snapshots
- Wrote the entire 16 MB state file atomically
- Repeated immediately without delay
## Impact
- System Performance: Severe memory pressure, 13 GB swap, system-wide slowdown
- Disk I/O: ~1.2 TB of unnecessary writes (SSD wear)
- User Experience: Fans at maximum, unresponsive system
- Required force kill (
kill -9)
## Reproduction Steps (uncertain)
The exact trigger is unclear, but the scenario was:
- Had multiple sessions with pasted images in history (~15 MB total)
- Ran
claude --continueto resume previous session - Process entered infinite write loop
## Workaround
Clean up large pasted images from session history:
```python
import json
data = json.load(open('~/.claude.json'))
for project in data.get('projects', {}).values():
for item in project.get('history', []):
if 'pastedContents' in item:
item['pastedContents'] = {}
This reduced file from 16 MB → 212 KB.
Suggested Fixes
- Rate limit state writes: Don't write state more than once per second
- Detect write loops: If same state written >10 times in 10 seconds, abort
- Limit history size: Cap pasted content size or auto-clean old items
- Add memory monitoring: Warn/exit if process exceeds reasonable memory threshold
- Fix --continue logic: Investigate why session restoration triggers write loop
Additional Context
- Process was running for 2:14:38 before detection
- Normal Claude Code process uses ~500 MB
- This appears related to session state management, not AI inference
What Should Happen?
Claude should handle its own clean up and memory management.
Error Messages/Logs
No specific error message shown. Fans maxed our and noticed in System Monitor the 2.0.28 process (weird that you don't name it Claude Code or the like), was using more than 20GB of memory.
Steps to Reproduce
The exact trigger is unclear, but the scenario was:
- Had multiple sessions with pasted images in history (~15 MB total)
- Ran
claude --continueto resume previous session - Process entered infinite write loop
Claude Model
Sonnet (default)
Is this a regression?
Yes, this worked in a previous version
Last Working Version
This was failing in 2.0.27 also
Claude Code Version
2.0.28 (Claude Code)
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Other
Additional Information
Using Ghostty shell app.
I was unsure how to solve this so used Claude Code to do it's own investigation in a new process.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗