Skip to content

fix: reduce memory and disk usage from harness subprocess artifacts#16

Merged
AbirAbbas merged 1 commit into
mainfrom
fix/memory-leak-cleanup
Mar 16, 2026
Merged

fix: reduce memory and disk usage from harness subprocess artifacts#16
AbirAbbas merged 1 commit into
mainfrom
fix/memory-leak-cleanup

Conversation

@AbirAbbas
Copy link
Copy Markdown
Contributor

Summary

  • 7 GB RSS on Railway traced to Python heap fragmentation (pymalloc never returns freed arenas to OS) and unbounded evidence re-extraction in the coverage loop
  • 2.7 GB disk waste from accumulated Claude Code session data (~1.14 GB in ~/.claude/projects/), V8 JIT .so files (~106 MB in /tmp), and pyright temp directories
  • Fixes are all concurrency-safe — designed for multiple simultaneous reviews in the same container

Changes

Memory (heap)

  • MALLOC_TRIM_THRESHOLD_=0 env var in Dockerfile so glibc aggressively returns freed pages
  • malloc_trim(0) called via ctypes after each review's GC pass
  • evidence_map and verification_map explicitly cleared after use in _run_review_layer
  • Coverage loop now only extracts evidence for new gap findings instead of re-extracting for all accumulated findings each iteration (was quadratic, now linear)
  • Explicit del gap_evidence between coverage iterations

Disk (session artifacts)

  • Snapshot-based Claude session cleanup: snapshots ~/.claude/projects/ before review, deletes only sessions created during this review afterward (safe for concurrent reviews)
  • Empty pyright-* temp dirs cleaned from /tmp
  • Orphaned V8 .so files older than 60s removed from /tmp (age threshold protects concurrent harness calls)

Investigation findings

Component Size Type
Python main process heap 6.8 GB RSS (peak 14.1 GB) RAM
.claude/projects/ session data 1.14 GB (49 sessions) Disk
.opencode/bin/ 135 MB Disk
/tmp/*.so V8 artifacts 106 MB (24 files) Disk
/tmp/pyright-* dirs ~40 empty dirs Disk
.npm + .bun caches 140 MB Disk

Test plan

  • ruff check passes on all changed files
  • Existing tests pass (test_staggered_gather.py)
  • Python AST parse verification for both changed files
  • Import verification with PYTHONPATH=src
  • Deploy to Railway and verify RSS drops after reviews complete
  • Run concurrent reviews and verify no cross-contamination of session cleanup

🤖 Generated with Claude Code

Railway deployment investigation revealed 7 GB RSS and 2.7 GB disk waste
from accumulated Claude Code session data, V8 JIT .so files, and Python
heap fragmentation after reviews.

Memory fixes:
- Set MALLOC_TRIM_THRESHOLD_=0 so glibc returns freed pages to OS
- Call malloc_trim(0) via ctypes after each review completes
- Clear evidence_map and verification_map after use in review layer
- Only extract evidence for new gap findings in coverage loop (was
  re-extracting for ALL accumulated findings each iteration)
- Explicit del of gap_evidence between coverage iterations

Disk fixes:
- Snapshot Claude session dirs before review, delete only new ones after
  (safe for concurrent reviews — each cleans only its own sessions)
- Clean empty pyright-* temp dirs from /tmp after reviews
- Remove orphaned V8 .so files older than 60s from /tmp

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@AbirAbbas AbirAbbas merged commit 4658417 into main Mar 16, 2026
2 checks passed
@AbirAbbas AbirAbbas deleted the fix/memory-leak-cleanup branch March 16, 2026 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant