< Back to all hacks

#57 kill-dalvik Cron (Auto-Free ~40 MB)

Desktop
Problem
Termux Dalvik VMs (com.termux ~49 MB + com.termux.boot ~40 MB) waste 89 MB after boot.
Solution
Cron kills com.termux.boot every 2 min. Only safe target — com.termux kill causes cgroup cascade killing gateway.
Lesson
Android cgroup cascade: killing com.termux kills ALL processes in its cgroup including the gateway.

Context

After Termux:Boot fires the boot script, two Dalvik VMs remain running: com.termux (49 MB) and com.termux.boot (40 MB). Together they consume 89 MB — almost as much as system_server. The boot script has already finished, so these VMs serve no purpose. However, only com.termux.boot is safe to kill.

Killing com.termux triggers Android's cgroup cascade: every process in the Termux cgroup (bash, sshd, Node.js gateway, cron) is killed with SIGKILL. The gateway was launched with setsid (Hack #33) which gives it its own session, but it's still in Termux's cgroup because setsid doesn't change cgroup membership.

Implementation

The cron job uses /system/bin/ps (not Termux's ps, which only shows current-TTY processes):

#!/data/data/com.termux/files/usr/bin/bash
# kill-dalvik.sh — kill com.termux.boot VM

PS=/system/bin/ps

# Only kill if gateway is running (safety check)
if ! $PS 2>/dev/null | grep -q "openclaw-gateway"; then
  exit 0
fi

# Kill com.termux.boot only (safe — separate package/cgroup)
$PS 2>/dev/null | grep "com.termux.boot$" | while read _USER PID _REST; do
  kill -9 $PID 2>/dev/null
done

Install the cron job:

# Add to crontab:
$PREFIX/bin/applets/crontab -e
# Add line:
*/2 * * * * $PREFIX/bin/kill-dalvik.sh

Verification

# Check com.termux.boot processes before:
/system/bin/ps | grep "com.termux.boot"
# Expected: 1-2 processes with ~40 MB

# Wait for cron to run (2 min), then:
/system/bin/ps | grep "com.termux.boot"
# Expected: no output (killed)

# Verify gateway is still running:
pgrep -f openclaw-gateway
# Expected: PID still exists

# Verify com.termux is NOT killed:
/system/bin/ps | grep "com.termux$"
# Expected: still running (we only kill .boot)

Gotchas

  • NEVER kill com.termux — the cgroup cascade kills the gateway, sshd, cron, and everything else. Only com.termux.boot is safe because it's a separate package with its own cgroup
  • Termux's built-in ps (procps) only shows processes attached to the current TTY. Must use /system/bin/ps which shows all system processes
  • awk doesn't exist in /system/bin/ — use shell builtins (while read) for field extraction
  • Android will respawn com.termux.boot periodically (service restart policy). The cron job kills it again within 2 minutes
  • Killing the Dalvik VM does NOT release the wake lock held by com.termux. The lock is managed by the main Termux process

Result

MetricBeforeAfter
com.termux.boot RAM~40 MB0 MB (killed)
Kill frequencyNeverEvery 2 min
Gateway impactN/ANone (different cgroup)
com.termux impactN/AUntouched (safe)