Managing Memory Leaks When Running Multiple Claude Code Agents
Managing Memory Leaks When Running Multiple Claude Code Agents
Running one Claude Code agent is fine. Running five in parallel on the same repo turns memory leaks from a minor annoyance into a machine-killing problem. Orphaned node processes pile up, and within a few hours your Mac is swapping to disk.
The Problem Multiplies
Each Claude Code session spawns multiple node processes - MCP servers, language servers, file watchers. When a session ends or crashes, some of these processes do not get cleaned up. With one agent, you might lose a few hundred megabytes. With five agents running continuously, you can lose gigabytes per day.
The symptoms are predictable: fans spin up, builds get slower, and eventually something gets OOM-killed.
The Cleanup Script
The fix is straightforward - a script that runs on a schedule and kills orphaned processes:
- Identify orphans - find node processes whose parent PID is 1 (adopted by init) or whose parent Claude Code process no longer exists
- Filter carefully - not every node process is an orphan. Check if the process is still attached to a running Claude session
- Kill and log - terminate the orphans and log what you killed so you can debug if something goes wrong
A cron job or launchd plist running this every 2-3 hours keeps things stable.
Prevention Helps Too
Beyond cleanup, a few habits reduce the problem:
- Close sessions cleanly rather than just closing the terminal
- Use separate terminal tabs (not background processes) so you can see what is running
- Monitor with Activity Monitor or
htopsorted by memory to catch problems early - Set memory limits on MCP servers where possible
The underlying issue is that long-running agent sessions were not designed for continuous operation. Until that changes, a cleanup script is essential infrastructure for anyone running parallel agents.
- Claude Code's Real Advantage Is the Harness
- Managing Parallel Claude Agents
- LLM Costs and Monthly Breakdown for Agents
Fazm is an open source macOS AI agent. Open source on GitHub.