Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to retrieve memory used(RAM) in percentage using linux command. 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 dont know how to parse this info or if there is any different command to get the memory used in percentage.

share|improve this question
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
Thanks to everyone! –  Robi Aug 26 '14 at 20:49
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

3 Answers 3

up vote 4 down vote accepted

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

To parse this, the first line of numbers (Mem:) lists total memory, used and free memory and the usage of shared, buffers and filesystem caches. 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 gives us the actual used and free memory as if there were no buffers or cache.

The final line 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}' 
share|improve this answer
Is "-/+ buffers/cache: 5779440 18904464" correct RAM usage/status of server? –  Robi Aug 26 '14 at 20:43
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
Thanks. That's exactly what I needed to know too. –  Robi Aug 26 '14 at 20:54

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.

share|improve this answer

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%
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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