#!/bin/bash # MacMelon Mac memory check # Read-only. Changes nothing, installs nothing, needs no admin password, sends nothing anywhere. # It prints app names, process names, sizes and ages. It never prints command lines # (they can contain API keys and database passwords), file paths or your username. # # Run it: bash mac-memory-check.sh # Guide: https://macmelon.com/guides/free-mac-memory-check-script set -u mb() { awk -v k="$1" 'BEGIN { printf "%.0f", k / 1024 }'; } echo "MacMelon Mac memory check ($(date '+%Y-%m-%d %H:%M'))" echo "========================================================" # --- This Mac ----------------------------------------------------------------- macos=$(sw_vers -productVersion 2>/dev/null) chip=$(sysctl -n machdep.cpu.brand_string 2>/dev/null) ram_gb=$(sysctl -n hw.memsize 2>/dev/null | awk '{ printf "%.0f", $1 / 1073741824 }') echo "Mac: $chip, ${ram_gb} GB memory, macOS $macos" echo "Up for: $(uptime | sed -E 's/^.*up +//; s/, +[0-9]+ users?.*$//')" # --- Memory pressure: the number that matters ---------------------------------- level=$(sysctl -n kern.memorystatus_vm_pressure_level 2>/dev/null) case "$level" in 1) pressure="Normal (green)" ;; 2) pressure="Warning (yellow): macOS is working hard to find room" ;; 4) pressure="Critical (red): macOS is out of easy options" ;; *) pressure="Unknown" ;; esac free_pct=$(memory_pressure 2>/dev/null | awk -F': ' '/free percentage/ { print $2 }') echo echo "MEMORY PRESSURE" echo " Now: $pressure" echo " Free (system-wide): ${free_pct:-unknown}" # --- Swap ---------------------------------------------------------------------- swap=$(sysctl -n vm.swapusage 2>/dev/null) swap_total=$(echo "$swap" | awk '{ print $3 }' | tr -d 'M') swap_used=$(echo "$swap" | awk '{ print $6 }' | tr -d 'M') echo echo "SWAP (memory macOS has written to disk)" echo " Used: ${swap_used:-?} MB of ${swap_total:-?} MB" vm_stat 2>/dev/null | awk ' /Swapins/ { gsub(/\./, "", $2); printf " Swap-ins since boot: %s pages read back from disk\n", $2 } /Swapouts/ { gsub(/\./, "", $2); printf " Swap-outs since boot: %s pages written to disk\n", $2 }' disk_free=$(df -g / 2>/dev/null | awk 'NR == 2 { print $4 }') echo " Disk free on startup volume: ${disk_free:-?} GB (swap needs room here; a nearly full disk makes freezes come sooner)" # --- Biggest apps ---------------------------------------------------------------- # RSS is what is in RAM right now. It leaves out memory macOS has compressed or swapped # out, and counts shared pages in every process that uses them. Treat the order as a # good guide and the sizes as rough; an app's true footprint can be several times its RSS # when the Mac is swapping. echo echo "BIGGEST APPS (approximate, grouped by app)" ps -axo rss=,comm= 2>/dev/null | awk ' { r = $1; $1 = ""; sub(/^ /, "") if (match($0, /\/[^\/]+\.app\//)) { app = substr($0, RSTART + 1, RLENGTH - 6); sum[app] += r } } END { for (a in sum) printf "%d\t%s\n", sum[a], a }' \ | sort -rn | head -10 \ | awk -F'\t' '{ printf " %6.0f MB %s\n", $1 / 1024, $2 }' # --- Helpers that often outlive their app ------------------------------------------ # Matches on the command line but prints only the process name, so nothing sensitive is shown. echo echo "HELPERS BY KIND (count, memory, longest running)" ps -axo pid=,rss=,etime=,command= 2>/dev/null | awk ' function kind(c) { if (c ~ /mcp-server|mcp-remote|[\/ _-]mcp[\/ _.-]|mcp-process/) return "MCP server" if (c ~ /tsserver|typescript-language-server|language[-_]server|gopls|rust-analyzer|pyright|sourcekit-lsp/) return "language server" if (c ~ /extension-host|extensionHost/) return "editor extension host" if (c ~ /[\/ ]ollama( |$)/) return "Ollama" if (c ~ /com\.docker|Docker Desktop|vpnkit|docker-?desktop/) return "Docker" if (c ~ /next-server|webpack|vite|esbuild|tsc --watch/) return "dev server / watcher" return "" } function secs(t, d, a, n) { d = 0 if (t ~ /-/) { split(t, a, "-"); d = a[1]; t = a[2] } n = split(t, a, ":") if (n == 3) return d * 86400 + a[1] * 3600 + a[2] * 60 + a[3] return d * 86400 + a[1] * 60 + a[2] } function human(s) { if (s >= 86400) return sprintf("%.1f days", s / 86400) if (s >= 3600) return sprintf("%.1f hours", s / 3600) return sprintf("%d min", s / 60) } { pid = $1; rss = $2; age = secs($3) $1 = ""; $2 = ""; $3 = ""; sub(/^ +/, "") k = kind($0); if (k == "") next cnt[k]++; mem[k] += rss; if (age > oldest[k]) oldest[k] = age } END { for (k in cnt) printf "%d\t%s\t%d\t%s\n", mem[k], k, cnt[k], human(oldest[k]) }' \ | sort -rn \ | awk -F'\t' '{ printf " %-22s %3d process%s %6.0f MB longest running %s\n", $2, $3, ($3 == 1 ? " " : "es"), $1 / 1024, $4 }' echo " (A kind that is running although you closed its app, or has run for days, is the lead to follow.)" if command -v ollama >/dev/null 2>&1; then echo echo "OLLAMA MODELS CURRENTLY LOADED" loaded=$(ollama ps 2>/dev/null | tail -n +2) if [ -z "$loaded" ]; then echo " none"; else echo "$loaded" | awk '{ print " " $0 }'; fi fi echo echo "HOW TO READ THIS" echo " - Pressure Normal and little swap growth: your Mac is fine, even if memory looks full." echo " - Pressure Warning or Critical, with swap still growing: look at the apps and helpers above." echo " - A helper running for days after you closed its app is the first thing to check." echo " - Quit apps normally (Cmd-Q) rather than killing processes. Nothing here was changed."