Workaround for opencode#15348 — unbounded heap growth + orphaned processes on Linux.
OpenCode processes don't handle SIGHUP, so when you close a terminal they become orphans and keep eating RAM forever. This script finds and kills them.
# Download
curl -fsSL https://gist.githubusercontent.com/OmerFarukOruc/d1bf2bb1f6f4f01c3a0e63288ada7c94/raw/oc-cleanup -o ~/.local/bin/oc-cleanup
chmod +x ~/.local/bin/oc-cleanup
# Add aliases to your shell rc (~/.zshrc or ~/.bashrc)
echo '# OpenCode cleanup aliases
alias oc-orphans="oc-cleanup"
alias oc-kill="oc-cleanup kill"
alias oc-watch="oc-cleanup watch"
alias oc-guard="oc-cleanup guard"' >> ~/.zshrc
source ~/.zshrcoc-orphans # List orphaned opencode processes (no TTY)
oc-kill # Kill all orphaned opencode processes
oc-watch # Show all live opencode processes with resource usage
oc-guard # Continuous watchdog — kill orphans over 1GB RAM (check every 60s)
oc-guard 512 # Custom threshold — kill orphans over 512MB
oc-guard 512 30 # Custom threshold (512MB) + check interval (30s)╭──────────────────────────────────────────────────────────╮
│ ⚠ Orphaned OpenCode Processes │
├──────────────────────────────────────────────────────────┤
│ 💀 2681180 49 MB 0.1% █░░░░░░░░░░░░░░░ 0.2% │
│ 💀 2869680 250 MB 9.4% █░░░░░░░░░░░░░░░ 0.8% │
├──────────────────────────────────────────────────────────┤
│ Total wasted: 298 MB (1.0% of system RAM) │
│ Run oc-kill to free it │
╰──────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────────────────────╮
│ 🗑 Killing Orphaned Processes │
├──────────────────────────────────────────────────────────┤
│ 💀 PID 2681180 │ 49 MB │ 0.1% CPU │
│ 💀 PID 2869680 │ 250 MB │ 9.4% CPU │
├──────────────────────────────────────────────────────────┤
│ ⚡ Killed 2 orphans — freed ~298 MB of RAM │
╰──────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────────────────────╮
│ 👁 OpenCode Process Monitor │
├──────────────────────────────────────────────────────────┤
│ 2835012 796 MB 35.5% pts/2 opencode │
│ 2869680 250 MB 9.4% ? opencode -s ses_35f… │
╰──────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────────────────────╮
│ 🛡 OpenCode Guard Mode │
├──────────────────────────────────────────────────────────┤
│ RAM threshold: 1024 MB per process │
│ Check interval: 60s │
│ System RAM: 32068 MB │
│ Press Ctrl+C to stop │
├──────────────────────────────────────────────────────────┤
│ ✓ [2025-03-01 14:30:00] No orphans │
│ 👁 [2025-03-01 14:31:00] PID 2869680 250 MB ok │
│ 💀 [2025-03-01 14:32:00] PID 2681180 1280 MB KILLED │
│ ⚡ Cycle: killed 1, freed ~1280 MB (total: 1) │
├──────────────────────────────────────────────────────────┤
│ 🛡 Guard stopped. Killed 1 total, freed ~1280 MB │
╰──────────────────────────────────────────────────────────╯
| Argument | Default | Description |
|---|---|---|
$2 — threshold |
1024 |
RAM threshold in MB. Orphans exceeding this get killed. |
$3 — interval |
60 |
Check interval in seconds. |
Run in the background with nohup:
nohup oc-cleanup guard 512 30 > /tmp/oc-guard.log 2>&1 &Or as a systemd user service:
mkdir -p ~/.config/systemd/user
cat > ~/.config/systemd/user/oc-guard.service << 'EOF'
[Unit]
Description=OpenCode orphan process guard
After=default.target
[Service]
ExecStart=%h/.local/bin/oc-cleanup guard 1024 60
Restart=on-failure
RestartSec=10
Environment=TERM=dumb
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable --now oc-guard# Kill orphans every 30 minutes
(crontab -l 2>/dev/null; echo "*/30 * * * * $HOME/.local/bin/oc-cleanup kill >/dev/null 2>&1") | crontab -OpenCode on Linux has a known bug where:
- Heap memory grows unboundedly — reaching 37+ GB in some cases
- Processes don't die when you close the terminal (no SIGHUP handler)
- Orphaned processes keep consuming RAM/CPU silently