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!
What you're looking for is the output from "free":
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:
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:
You can see that vmstat is showing all the same numbers as free.
I have same question with you and come up with a formula
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):
Output (on my system):
Same thing, with
free
: