10

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%.

Thanks!

womble
  • 95,029
  • 29
  • 173
  • 228
His
  • 249
  • 1
  • 4
  • 9

4 Answers4

21

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.

Evan Anderson
  • 141,071
  • 19
  • 191
  • 328
2

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

1

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".

Aaron Bush
  • 399
  • 1
  • 4
0

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)) ; } ; }
vurp

Output (on my system):

12

Same thing, with free:

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