Skip to content

Instantly share code, notes, and snippets.

@apoorvalal
Created September 7, 2025 16:51
Show Gist options
  • Save apoorvalal/493dd38b31420f8395c5751656117964 to your computer and use it in GitHub Desktop.
Save apoorvalal/493dd38b31420f8395c5751656117964 to your computer and use it in GitHub Desktop.

automated reclaiming RAM from macos' pesky background services

Edit ramcleaner.sh to kill the processes that tend to balloon RAM usage. medianalysisd is the main culprit; it often gobbles up 3-4GB RAM in random spikes, which is prohitive on the base mac mini 16GB that I run as a home-server.

This bash script searches for the service by prgrepping active processes and kills what it finds. When OS services are observationally equivalent to viruses, they ought to be treated accordingly.

Schedule it by running crontab -e and entering the following, which runs the cleanup script every 2 minutes and logs its actions to ~/.ramcleaner.log.

*/2 * * * * /bin/bash ramcleaner.sh >/dev/null 2>&1
#!/bin/bash
# ramcleaner
# Terminates mediaanalysisd process if running
# Designed for cron execution with proper error handling
# Set strict error handling
set -euo pipefail
# Log file location (optional - comment out if not needed)
LOG_FILE="$HOME/.ramcleaner.log"
# Function to log messages with timestamp
log_message() {
if [ -n "${LOG_FILE:-}" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
fi
}
# Function to kill process by name
kill_process() {
local process_name="$1"
local pids
# Get PIDs of the target process
# Using pgrep for reliability (more robust than ps | grep)
if command -v pgrep >/dev/null 2>&1; then
pids=$(pgrep -x "$process_name" 2>/dev/null || true)
else
# Fallback to ps if pgrep not available
pids=$(ps aux | grep -E "^[^ ]*[ ]+[0-9]+.*[^]]${process_name}$" | awk '{print $2}' 2>/dev/null || true)
fi
if [ -n "$pids" ]; then
for pid in $pids; do
# Verify the process exists before killing
if kill -0 "$pid" 2>/dev/null; then
# First try SIGTERM (graceful termination)
if kill -TERM "$pid" 2>/dev/null; then
log_message "Sent SIGTERM to $process_name (PID: $pid)"
# Give it a moment to terminate gracefully
sleep 1
# If still running, force kill with SIGKILL
if kill -0 "$pid" 2>/dev/null; then
kill -KILL "$pid" 2>/dev/null || true
log_message "Force killed $process_name (PID: $pid) with SIGKILL"
fi
else
log_message "Failed to kill $process_name (PID: $pid) - may require sudo"
fi
fi
done
return 0
else
# Process not found - this is normal and expected most of the time
return 1
fi
}
# Main execution
main() {
# Kill mediaanalysisd
if kill_process "mediaanalysisd"; then
log_message "Successfully terminated mediaanalysisd"
fi
# Also kill related processes that might respawn it
# photolibraryd and photoanalysisd are related services
kill_process "photolibraryd" 2>/dev/null || true
kill_process "photoanalysisd" 2>/dev/null || true
# Optional: Clear cache to prevent immediate respawn
# Uncomment if you want more aggressive prevention
# rm -rf ~/Library/Caches/com.apple.mediaanalysisd 2>/dev/null || true
}
# Execute main function
main
# Exit successfully even if no process was found
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment