Skip to content

Instantly share code, notes, and snippets.

@icedac
Last active June 30, 2025 05:16
Show Gist options
  • Save icedac/e162d087e0f5d5b189c63e844b03d8b3 to your computer and use it in GitHub Desktop.
Save icedac/e162d087e0f5d5b189c63e844b03d8b3 to your computer and use it in GitHub Desktop.
memcpu
setopt interactivecomments
# ──────────────────────────────────────────────────────────
# 1 실시간 메모리·CPU 상위 N개 출력 ─ memcpu
memcpu() {
local L=30 nameMax=30 pathMax=30
while [[ $# -gt 0 ]]; do
case "$1" in
-trim)
IFS=',' read -r nameMax pathMax <<< "$2"; shift 2 ;;
[0-9]*)
L=$1; shift ;;
*) shift ;;
esac
done
ps -axo rss,pcpu,args | \
awk -v L="$L" -v NM="$nameMax" -v PM="$pathMax" '
NR==1 {next}
{
rssKB=$1; cpu=$2
$1=$2=""; sub(/^ +/, "", $0)
split($0, tok, " "); path = tok[1]
idx=1
while (path !~ /\.app\// && ++idx <= length(tok))
path = path " " tok[idx]
if (match(path, /([^\/]+)\.app\//, cap))
gkey = cap[1]
else { n = split(path, pp, "/"); gkey = pp[n] }
sub("^" path "[ ]*", "", $0)
split(path, bp, "/"); exec = bp[length(bp)]
name = exec (length($0) ? " " $0 : "")
if (length(name) > NM) name = substr(name,1,NM-1)"…"
dpath = (length(path) > PM) ? "…"substr(path, length(path)-PM+1) : path
rssMB = rssKB/1024
printf "I\t%012.2f\t%-*s\t%8.1f MB\t%6.1f%%\t%-*s\n",
rssMB,NM,name,rssMB,cpu,PM,dpath
g_mem[gkey]+=rssMB; g_cpu[gkey]+=cpu; g_cnt[gkey]++
}
END {
for (g in g_mem) {
mem=g_mem[g]; c=g_cpu[g]; k=g_cnt[g]
gname=(length(g)>30?substr(g,1,27)"…":g)
printf "G\t%012.2f\t%-30s\t%8.1f MB\t%6.1f%%\t%3d\n",
mem,gname,mem,c,k
}
}' | \
LC_ALL=C sort -t$'\t' -k2,2nr | \
awk -F'\t' -v L="$L" '
BEGIN {
print "─ INDIVIDUAL (Top " L " by RealMem) ───────────────────────────────"
printf "%-30s %10s %7s %-30s\n","Process","RealMem","CPU%","Path"
}
$1=="I" && ++ic<=L { printf "%s\t%s\t%s\t%s\n",$3,$4,$5,$6 }
{ lines[NR]=$0 }
END {
print "\n─ GROUPED by Executable (Top " L " by RealMem) ───────────────"
printf "%-30s %10s %7s %5s\n","Executable","TotMem","CPU%","Cnt"
gc=0
for(i=1;i<=NR;i++)
if(substr(lines[i],1,1)=="G" && ++gc<=L){
split(lines[i],f,"\t")
printf "%s\t%s\t%s\t%s\n",f[3],f[4],f[5],f[6]
}
}'
}
# 사용 예
# memcpu → Top 30, 이름·경로 30자
# memcpu 50 -trim 50,40 → Top 50, 이름 50자, 경로 40자
# ──────────────────────────────────────────────────────────
# 2 Edge 렌더러 대량 점유 시 강제 종료 ─ memfree
alias memfree='ps aux | grep "Microsoft Edge Helper (Renderer)" | grep -v grep | awk '\''{print $2}'\'' | xargs kill -9'
# 3 iOS 시뮬레이터 메모리 청소 ─ simclean
simclean() {
echo "▶ Shutting down & erasing all simulators..."
xcrun simctl shutdown all
xcrun simctl erase all
echo "▶ Killing residual simulator services..."
launchctl kickstart -k gui/$(id -u)/com.apple.CoreSimulator.CoreSimulatorService
pkill -f "(Simulator.app|Sim(MetalHost|RenderServer)|xcodebuild)"
echo "▶ Done. Current booted devices:"
if xcrun simctl list devices | grep -q Booted; then
xcrun simctl list devices | grep Booted
else
echo "None ✅"
fi
}
# ──────────────────────────────────────────────────────────
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment