[BUG] Memory leak and runaway write loop in `claude --continue` causes 28GB memory usage

Resolved 💬 4 comments Opened Oct 28, 2025 by TonyMilton Closed Jan 10, 2026

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

  1. Process PID 42914 running for 2+ hours with high CPU (382.8%)
  2. Memory consumption: 27.87 GB (actual RAM, not virtual)
  3. System memory pressure: 32.73/36 GB used + 13.11 GB swap
  4. 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:

  1. Checked for hooks (finding none)
  2. Created file history snapshots
  3. Wrote the entire 16 MB state file atomically
  4. 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:

  1. Had multiple sessions with pasted images in history (~15 MB total)
  2. Ran claude --continue to resume previous session
  3. 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

  1. Rate limit state writes: Don't write state more than once per second
  2. Detect write loops: If same state written >10 times in 10 seconds, abort
  3. Limit history size: Cap pasted content size or auto-clean old items
  4. Add memory monitoring: Warn/exit if process exceeds reasonable memory threshold
  5. 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:

  1. Had multiple sessions with pasted images in history (~15 MB total)
  2. Ran claude --continue to resume previous session
  3. 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.

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗