I am trying to retrieve memory used(RAM) in percentage using Linux commands. My cpanel shows Memory Used which I need to display on a particular webpage.

From forums, I found out that correct memory can be found from the following:

free -m


-/+ buffers/cache:        492       1555

-/+ buffers/cache: contains the correct memory usage. I don't know how to parse this info or if there is any different command to get the memory used in percentage.

  • 3
    That was answered over here already! stackoverflow.com/questions/10585978/… e.g: free | grep Mem | awk '{print $4/$2 * 100.0}' – szboardstretcher Aug 26 '14 at 20:30
  • @szboardstretcher I like this one: $ free | awk '/Mem/{printf("used: %.2f%"), $3/$2*100} /buffers\/cache/{printf(", buffers: %.2f%"), $4/($3+$4)*100} /Swap/{printf(", swap: %.2f%"), $3/$2*100}' – Simply_Me Aug 26 '14 at 20:43
  • Without knowing what you need the number for it is hard to tell you what number you should use. However in many cases any of those numbers is just very naive and requires trend and context information. – eckes Aug 26 '14 at 23:44

Here is sample output from free:

% free
             total       used       free     shared    buffers     cached
Mem:      24683904   20746840    3937064     254920    1072508   13894892
-/+ buffers/cache:    5779440   18904464
Swap:      4194236        136    4194100

The first line of numbers (Mem:) lists

  • total memory
  • used memory
  • free memory
  • usage of shared
  • usage of buffers
  • usage filesystem caches (cached)

In this line used includes the buffers and cache and this impacts free. This is not your "true" free memory because the system will dump cache if needed to satisfy allocation requests.

The next line (-/+ buffers/cache:) gives us the actual used and free memory as if there were no buffers or cache.

The final line (Swap) gives the usage of swap memory. There is no buffer or cache for swap as it would not make sense to put these things on a physical disk.

To output used memory (minus buffers and cache) you can use a command like:

% free | awk 'FNR == 3 {print $3/($3+$4)*100}'

This grabs the third line and divides used/total * 100.

And for free memory:

% free | awk 'FNR == 3 {print $4/($3+$4)*100}' 
  • 1
    Is "-/+ buffers/cache: 5779440 18904464" correct RAM usage/status of server? – codelogn Aug 26 '14 at 20:43
  • 2
    Strictly speaking, no, but practically speaking, yes. The actual memory usage is the 2nd line (used: 20746840) but part of that memory use is buffers and cache which are just to speed up response times on your system. The third line (used: 5779440) gives you the amount of memory actually used by programs and it is this memory figure that determines how much more your programs can allocate. If your goal is to get free / used memory in a sense of what programs are actually using and what you can actually allocate, then yes, line 3 is correct for you. – casey Aug 26 '14 at 20:51
  • 1
    Note that with more recent versions of free, -/+ buffers/cache is completely omitted intentionally, and the value shown in the used column now excludes the buffers and cache by default. See here: bugzilla.redhat.com/show_bug.cgi?id=1158828 – jdgregson Mar 9 '18 at 6:31

I am going to mention on how to parse the information on free command. To find the percentage, you can use as suggested in the other 2 answers.

This is clearly explained here. I would try to explain from what I have in my system.

free -m
             total       used       free     shared    buffers     cached
Mem:          7869       4402       3466          0        208       3497
-/+ buffers/cache:        696       7173
Swap:         3999        216       3783

Now, let us see what the various numbers actually represent.


  • 7869 Indicates memory/physical RAM available for my machine.
  • 4402 Indicates memory/RAM used by my system.
  • 3466 Indicates Total RAM free and available for new process to run.
  • 0 Indicates shared memory. This column is obsolete and may be removed in future releases of free.
  • 208 Indicates total RAM buffered by different applications in Linux.
  • 3497 Indicates total RAM used for Caching of data for future purpose.


-/+ buffers/cache:        696       7173

How to calculate the values 696 and 7173 obtained in Line2?

Total used (4402) - Total buffer RAM (208) - Total RAM for caching(3497) should constitute the Actual used RAM in the system. It returns me 697 which is the actual used RAM output in the second line.

Now, Total Available (7869) - Actual Used (696) should give you the free memory which is 7173 in this case which is also got as output in the second line.


Though a duplicate as pointed by @szboardstretcher , my preference from the solutions (in the original question) is the one below, specially since you want to parse to a webpage.

$ free | awk '/Mem/{printf("used: %.2f%"), $3/$2*100} /buffers\/cache/{printf(", buffers: %.2f%"), $4/($3+$4)*100} /Swap/{printf(", swap: %.2f%"), $3/$2*100}'


used: 82.68%, buffers: 42.81%, swap: 1.27%
  • 4
    FYI I get the following using this used: 96.00%, buffers: 26.53%awk: (FILENAME=- FNR=4) fatal: division by zero attempted – kellyfj Jan 27 '16 at 21:49
  • What's the full command used? I checked it in centos. – Simply_Me Jan 29 '16 at 21:05
  • Sorry for the formatting but here you go $ cat /etc/issue CentOS release 6.7 (Final) $ free | awk '/Mem/{printf("used: %.2f%"), $3/$2*100} /buffers\/cache/{printf(", buffers: %.2f%"), $4/($3+$4)*100} /Swap/{printf(", swap: %.2f%"), $3/$2*100}' used: 12.91%, buffers: 96.24%awk: (FILENAME=- FNR=4) fatal: division by zero attempted $ free total used free shared buffers cached Mem: 15429704 1991416 13438288 164 151556 1259584 -/+ buffers/cache: 580276 14849428 Swap: 0 0 0 – kellyfj Jan 30 '16 at 13:38
  • For some versions of awk (mawk), the percent sign needs to be doubled to print it, otherwise awk throws an error, like this: printf("used: %.0f%%"), $3/$2*100 This works with both mawk and gawk. – Ján Lalinský Apr 7 '18 at 14:19
free | awk '/Mem/{printf("used: %.2f%"), $3/$2*100} /buffers\/cache/{printf(", buffers: %.2f%"), $4/($3+$4)*100} /Swap/{printf(", swap: %.2f%\n"), $3/$2*100}' 

Adds a new line after the swap number


For upto CentOS 7 You can try

cat /proc/meminfo 

to see real usage of memory by processes.

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.