3

The free commands gives the amount of free memory in two forms: completely free memory (the Mem line) and disposable memory used for caches and buffers (the -/+ buffers/cache line):

-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

In this example, there are 884 MB of free memory excluding buffers and cache, and 3422 MB of free memory including buffers and cache.

How can I get the second measurement (free memory, including buffers and cache) in sar?

1 Answer 1

10

The -r flag for sar will give you kbmfree, kbmemused, kbbuffers, and kbcached. It doesn't, unfortunately, total them like the -/+ buffers/cache column like the free command, so you will have to pipe it through an awk or perl script to total them. Try this:

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)
    }
}'
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .