基本原則是檢視 /proc/$pid/smaps 的內容,以下提供一個簡單的Script。
1 #!/bin/bash 2 3 pid=$1 4 if [ -f /proc/$pid/smaps ]; then 5 echo "* Mem usage for PID $pid" 6 echo "-- Size:" 7 cat /proc/$pid/smaps | grep -m 1 -e ^Size: | awk '{print $2}' 8 echo "-- Rss:" 9 cat /proc/$pid/smaps | grep -m 1 -e ^Rss: | awk '{print $2}' 10 echo "-- Pss:" 11 cat /proc/$pid/smaps | grep -m 1 -e ^Pss: | awk '{print $2}' 12 echo "Shared Clean" 13 cat /proc/$pid/smaps | grep -m 1 -e '^Shared_Clean:' | awk '{ 14 print $2}' 15 echo "Shared Dirty" 16 cat /proc/$pid/smaps | grep -m 1 -e '^Shared Dirty:' | awk '{ 17 print $2}' 18 fi
參考資料:
- http://superuser.com/questions/102005/how-can-i-display-the-memory-usage-of-each-process-if-i-do-a-ps-ef
- http://stackoverflow.com/questions/9922928/what-does-pss-mean-in-proc-pid-smaps
- http://unix.stackexchange.com/questions/33381/getting-information-about-a-process-memory-usage-from-proc-pid-smaps