linux上使用shell脚本查看内存使用率
如上的内存利用率 = 【-/+buffers/cache:used】/【Mem:total 】 = 6293404 / 16333656
对于free命令而言,有如下公式:
total = used + free;
(-buffers/cache) used = Mem:used - Mem:buffers -
Mem:cached;
(-buffers/cache) used= ( Mem:total - Mem:free ) - Mem:buffers -
Mem:cached
(+buffers/cache) free = Mem:free + Mem:buffers + Mem:cached
因此:(-buffers/cache) used内存数:6293404 = 真实内存占用
而+buffers/cache反映的是可以挪用的内存总数,并非目前真真正正使用的内存。
#!/bin/bash
#test_MemoryUsageShell
while :
do
phymem=`free | grep "Mem:" |awk '{print $2}'`
phymemused=`free | grep 'buffers/cache' | awk '{print $3}'`
# echo $phymem
# echo $phymemused
awk 'BEGIN{printf"%.2f%\n",('$phymemused'/'$phymem')*100}'
sleep 3
done