I need to get a memory usage in % from vmstat, but I don't quite understand some of the numbers relating to memory displayed in vmstat.

In particular, given an output like this

procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0 139728  45396 199128 1236360    1    2     4   622    3    1 51  4 43  2  0

What is the amount of total memory of the system? Is it 'swpd' + 'free' + 'buff' + 'cache', which total to 1620612?

What is the free amount of memory of the system? Is it 45396, as displayed in the 'free' column?

The problem is they don't add up or even look close to what 'top' reported. The output of 'top' which I took around the same time as 'vmstat's above is:

Mem:   4059616k total,  3965504k used,    94112k free,   200020k buffers

So...I am assuming there must be hidden numbers/options in 'vmstat' that I didn't know of?

I need to use 'vmstat' for this purpose and not other commands as this is what the requirement stated.

So ideally, what I want to achieve from 'vmstat' is the % memory usage, as in:

<used_memory>/<total_memory> * 100%.



What you're looking for is the output from "free":

$ free
             total       used       free     shared    buffers     cached
Mem:        775556     759456      16100          0      22132     592484
-/+ buffers/cache:     144840     630716
Swap:       500344      21972     478372

Here's a tour:

This is a box w/ 768MB of physical RAM and a 500344KB swap partition.

759456KB is "used" (second column, top row). Of this "used" memory, 23132KB is buffers (5th column, top row) and 592484KB (sixth column, top row) is cache, leaving 144840KB (2nd column, 2nd row) of physical memory that's being used by active processes.

When you consider that the memory used by buffers and cache could be used for processes, that leaves 630716KB (3rd column, 2nd row) of physical memory free.

The swap partition, as I said, is 500344KB (1st column, bottom row). 21972KB (2nd column, bottom row) of the swap file is in use, leaving 478372KB (3rd column, bottom row) free.

So, your definition of % free memory depends on whether you're counting buffers and cache or not, and whether you're counting swap or not.

That ought to give you enough to go on to calculate a percentage as you see fit.

Here's the output of top and free:

$ top -n1 | grep "used," ; free
Mem:    775556k total,   751472k used,    24084k free,    20776k buffers
Swap:   500344k total,    21972k used,   478372k free,   586648k cached

             total       used       free     shared    buffers     cached
Mem:        775556     751344      24212          0      20776     586648
-/+ buffers/cache:     143920     631636
Swap:       500344      21972     478372

You can see how top and free agree (albeit with a little difference-- this is a multi-user machine and the memory usage of the top and free programs are reflected in there).

Add a "-s" when you call vmstate, and you will see much the same output, just in a different form:

$ vmstat -s ; free
       775556  total memory
       759920  used memory
       674680  active memory
        18440  inactive memory
        15636  free memory
        21892  buffer memory
       594372  swap cache
       500344  total swap
        21972  used swap
       478372  free swap
       ... output truncated ...

             total       used       free     shared    buffers     cached
Mem:        775556     759920      15636          0      21892     594372
-/+ buffers/cache:     143656     631900
Swap:       500344      21972     478372

You can see that vmstat is showing all the same numbers as free.

  • Best answer I've found yet on this convoluted subject! – rinogo Jun 13 '13 at 0:22

I have same question with you and come up with a formula

vmstat -s | awk  ' $0 ~ /total memory/ {total=$1 } $0 ~/free memory/ {free=$1} $0 ~/buffer memory/ {buffer=$1} $0 ~/cache/ {cache=$1} END{print (total-free-buffer-cache)/total*100}'

I have tested it on vmstat version from procps-ng 3.3.10 and vmstat from procps-ng 3.3.9


I would recommend that you read through this IBM Redbook "Linux Performance and Tuning Guidelines" (http://www.redbooks.ibm.com/abstracts/redp4285.html?Open). It has details on the data returned by numerous commands and should help you.

I have typically used the "free" command in Linux or "cat /proc/meminfo".


Here's a POSIX shell function that rounds the desired percent down to the nearest integer (for scripting purposes this may be more useful, since POSIX shell arithmetic compare tests only work with integers):

# Vmstat (derived) Used Ram Percent
vurp() { vmstat -s | { read a b ; read c d ; echo $((100*$c/$a)) ; } ; }

Output (on my system):


Same thing, with free:

furp() { free | { read z; read a b c d; echo $((100*$c/$b)) ; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.