该free
命令以两种形式提供可用内存量:完全可用内存(线Mem
)和用于缓存和缓冲区的可支配内存(-/+ buffers/cache
线):
-bash-3.2$ free -m
total used free shared buffers cached
Mem: 16057 15173 884 0 17 2520
-/+ buffers/cache: 12635 3422
Swap: 12287 4937 7350
在此示例中,不包括缓冲区和缓存的可用内存为 884 MB,包括缓冲区和缓存的可用内存为 3422 MB。
我如何获得第二个测量值(可用内存,包括缓冲区和缓存)sar
?
答案1
标志-r
将为sar
您提供 kbmfree、kbmemused、kbbuffers 和 kbcached。遗憾的是,它不会像命令中的 -/+ buffers/cache 列那样将它们总计起来free
,因此您必须通过 awk 或 perl 脚本对其进行管道传输以将它们总计起来。试试这个:
sar -r |\
awk '
{
if (NR > 3 && $3 != "kbmemfree" && $3 != "") {
if ($1 != "Average:") {
kbmemfree = $3
kbmemused = $4
kbbuffers = $6
kbcached = $7
}
else {
kbmemfree = $2
kbmemused = $3
kbbuffers = $5
kbcached = $6
}
kbfree_plus_bufcache = kbmemfree + kbbuffers + kbcached
kbused_minus_bufcache = kbmemused - kbbuffers - kbcached
printf("%s %15d %15d\n", $0, kbfree_plus_bufcache, kbused_minus_bufcache)
} else if ($3 == "kbmemfree") {
printf("%s %15s %15s\n", $0, "kbfree+bufcache", "kbused-bufcache")
} else {
printf("%s\n", $0)
}
}'