5

I am trying to write a script to monitor memory status. Here is what I have so far:

# Memory Usage
ramactive=$(vm_stat | grep "Pages active" | while read a b c; do echo "$((${c%?}*4096/1024/1024))"; done)
ramwired=$(vm_stat | grep "Pages wired" | while read a b c d; do echo "$((${d%?}*4096/1024/1024))"; done)
ramspec=$(vm_stat | grep "Pages speculative" | while read a b c; do echo "$((${c%?}*4096/1024/1024))"; done)
raminactive=$(vm_stat | grep "Pages inactive" | while read a b c; do echo "$((${c%?}*4096/1024/1024))"; done)
ramfree=$(vm_stat | grep "Pages free" | while read a b c; do echo "$((${c%?}*4096/1024/1024))"; done)
rampurge=$(vm_stat | grep "Pages purgeable" | while read a b c; do echo "$((${c%?}*4096/1024/1024))"; done)

printf "RAM\n"
printf "%-11s%'.f MB Used\n" "Active:" "$ramactive"
printf "%-11s%'.f MB Used\n" "Wired:" "$ramwired"

printf "%-11s%'.f MB Used\n" "Inactive:" "$raminactive"
printf "%-11s%'.f MB Used\n" "Spec:" "$ramspec"
printf "%-11s%'.f MB Used\n" "Cached:" "$(($ramspec+$raminactive))"

printf "%-11s%'.f MB Used\n" "Free:" "$ramfree"

printf "%-11s%'.f MB Used\n" "Total Used:" "$(($ramwired+$ramactive))"
printf "%-11s%'.f MB Used\n" "Used-Purge:" "$(($ramwired+$ramactive-$rampurge))"
printf "%-11s%'.f MB Used\n" "Total Free:" "$(($ramfree+$ramspec+$raminactive))"
printf "%-11s%'.f MB Used\n" "Total RAM:" "$(($ramactive+$ramwired+$ramspec+$raminactive+$ramfree))"

I am just experimenting here, so it's not the final script. But I can't get the numbers from here and the Activity monitor to match. For instance, Active + Wired memory here shows to be 9 GB on my computer, while Activity monitor claims it's only 6.5 GB. The Wired memory reading matches, but not the Active reading. What am I missing here?

0

You must log in to answer this question.

Browse other questions tagged .