How to get CORRECT memory usage for Linux

One of the most common mistakes made by Linux users, is to run free (Mem: line) or sar to get memory utilization.   The memory used fields include the space used by the OS to cache files and directory info cace, which is use to speed up processing of frequently used files.


This does NOT mean that cached and buffer space can not used for applications memory utilization.   If memory is needed, the older entries are dropped.  To get the correct values, the cache and buffers space should be subtracted from the used memory.


For sar the output of “sar -r” can be uploaded to Sar Grapher to see a graphical representation of memory utilization.   Or to see the correct usage for the free command use the command below:

free | awk '/Mem/{printf("Mem used: %d - %.2f%, cache used (still available): %d - %.2f%"), ($3-$7-$6), ($3-$7-$6)/$2*100, $6+$7, ($6+$7)/$3*100} /Swap/{printf(", swap used: %d - %.2f%\n"), $3, $3/$2*100}'