Terminal Heroes 8: Memory usage scripts
I have a low-memory VPS running online and thus always have to worry about what uses how much memory… Of course top and my favorite htop are very good tools to check this, but sometimes I just want to get a super simple report if I’m overusing memory or not yet. The bellow scripts (most of them was found online or hacked up from multiple scripts to fit my needs) print simple yet very readable memory usage information.
memused
-
-
#if [ $1 = '']; then
-
# ps auxf | awk '{sum=sum+$6}; END {print sum/1024}';
-
#else
-
# ps auxf | grep $1 | awk '{sum=sum+$6}; END {print sum/1024}';
-
#fi
-
bean=`cat /proc/user_beancounters` guar=`echo "$bean" | grep vmguar | awk '{ print $4;}'` burst=`echo "$bean" | grep privvm | awk '{ print $5;}'` priv=`echo "$bean" | grep privvm | awk '{ print $2;}'`
-
let total=guar/256 let used=priv/256 let burst=burst/256
-
echo "VPS memory usage:"
-
echo "Used: $used MB"
-
echo "Total: $total MB"
-
echo "Burstable to: $burst MB"
Sample output:
VPS memory usage:
Used: 296 MB
Total: 256 MB
Burstable to: 512 MB
memfree
-
#
-
# Revised 02-Feb-2007: include kernel memory (kmemsize) in 'used' calculation
-
# and show percentages in output.
-
#
-
-
BEAN=`cat /proc/user_beancounters`
-
GUAR=`echo "$BEAN" | grep vmguar | awk '{ print $4;}'`
-
PRIV=`echo "$BEAN" | grep privvm | awk '{ print $2;}'`
-
KMEM=`echo "$BEAN" | grep kmem | awk '{ print $3;}'`
-
-
let TOTL=$GUAR/256
-
-
let KMMB=$KMEM/1048576
-
let PVMB=$PRIV/256
-
let USED=$KMMB+$PVMB
-
let FREE=$TOTL-$USED
-
-
if [ "$FREE" -gt "0" ]; then
-
let UPER=$USED*100/$TOTL
-
let FPER=100-$UPER
-
else
-
let UPER="100"
-
let FPER="0"
-
fi
-
-
echo "VPS Memory:"
-
echo " Total: $TOTL mb Used: $USED mb (${UPER}%) Free: $FREE mb (${FPER}%)"
Sample output:
VPS Memory:
Total: 256 mb Used: 302 mb (100%) Free: -46 mb (0%)
memheld
-
grep oomguarpages /proc/user_beancounters | awk '{s=$2;t=$3;u=$4; {print "VPS Memory Usage\nCurrent Held: " s/256 "MB\nMax Held: " t/256 "MB\nBarrier: "u/256"MB" }}'
Sample output:
VPS Memory Usage
Current Held: 160.832MB
Max Held: 294.605MB
Barrier: 256MB
memps
-
-
echo "ps axo pmem,pcpu,comm | sort"
-
ps axo pmem,pcpu,comm | sort
Sample output:
ps axo pmem,pcpu,comm | sort
0.0 0.0 script
0.0 0.0 script
0.1 0.0 init
#...
2.2 0.0 httpd
4.0 0.0 mysqld
7.3 0.0 httpd
8.0 0.0 httpd
%MEM %CPU COMMAND
memprivmpages
-
-
beans=`cat /proc/user_beancounters | grep priv`
-
max=`echo $beans | awk '{ print $4;}'`
-
use=`echo $beans | awk '{ print $2;}'`
-
let "per=$use*100/$max"
-
let "mb=$use/256"
-
let "mmb=$max/256"
-
-
echo "privvmpages usage: $mb MB ($per% of $mmb)"
Sample output:
privvmpages usage: 296 MB (57% of 512)
For those wondering, yes I do have memory usage above “100%” in the output of some of these scripts, and it’s partially true. A VPS does usually have some guaranteed memory, and some “burstable” – so you can use more than just the guaranteed memory for some period of time. On the mashine I ran this I have 256 guaranteed and a “total” of 512MB – including the burstable memory.
If you like any of these, feel free to use them. I use them by creating a scripts directory and adding it to the PATH, so I can simply invoke them from anywhere.
PS: For more detailed memory usage information (as graphs etc), I’m using Munin and really recommend it. The graphs it draws are really nice, and it doesn’t inflict heavy load on the server, and it can monitor remote hosts if needed!


Post a reply