2

Looking to do a one liner to tell the total amount of free memory on system including swap and cache.

free -t isn't an option so I have to do a sum of the values under the free column.

I'm not sure how I can get the values; if I use awk, how can I reference different numbers in different lines (free Mem is $3, whereas the free swap space for example on a different line is $4)?

  • Don't know why you want include buffers/cache in memory, you shouldn't count this (indeed total row doesn't count) because is you sum used+free of cached/buffers row, it's equal to total RAM. So the only sense to measure is Mem and Swap rows, and that is what Total row do. ¿? – periket2000 Oct 1 '15 at 13:59
  • fair enough but i want to include them for my script – johndoe12345 Oct 1 '15 at 14:02
1

on the debian I have in front of me right now, free -t shows a Total: row:

             total       used       free     shared    buffers     cached
Mem:       8197980    2583348    5614632          0     222508    1904352
-/+ buffers/cache:     456488    7741492
Swap:      7807584          0    7807584
Total:    16005564    2550180   13455384

If you just need that row, you could use tail and awk to grab the values.

However, if your free does not show this row, you could use awk with RS="" to join the lines:

free | awk -v RS="" '{print $10 "+" $17 "+" $21}' | bc

If the column layout of your free is different to mine, you may have to fidget with the field numbers.

Explanation: The RS field is the record separator. Setting this to "" joins all the lines in the output of free, so awk can reference them as if they were on one 'line'.

Counting from the beginning, $1 to $6 are the column headers, $7 is the Mem: row header, $8 to $10 are the values on that row... Therefore $10 is the free memory value. This is done in a similar way for the following values;

             total       used       free     shared    buffers     cached
Mem:            $8         $9        $10        $11        $12        $13
-/+ buffers/cache:        $16        $17
Swap:          $19        $20        $21

The awk command {print $10 "+" $17 "+" $21} basically joins these values with a + in between, and the output is piped to bc to calculate the sum.

  • can you explain this code please? and also can you show output of free and explain what $10 etc is referring to ? – johndoe12345 Oct 1 '15 at 14:05
  • Since I can't yet comment on @user1717828's answer, I'll have to do it here. The problem with using just awk '{print $3}' is that the '+/- cache' row label includes a space and therefore $3` on that row is the used column, not the free one. – plonk Oct 1 '15 at 14:06
  • yep i agree , that is one of my issues – johndoe12345 Oct 1 '15 at 14:08
  • @plonk, thanks! I was scratching my head on that. I updated the answer. Out of curiosity, why couldn't you comment on my answer? – user1717828 Oct 1 '15 at 14:13
  • @user1717828 I don't have enough reputation for that yet :( – plonk Oct 1 '15 at 14:15
1

perhaps this could be an alternative to free

cat /proc/meminfo | grep -e MemFree -e Buffers -e SwapFree | gawk 'BEGIN{s=0}{s+=$2}END{print s}'

1

Are you able to run free and then parse the output, like

free | awk '{print $4}' | tail -3 | sed '2d' |paste -sd+ | bc
  • @johndoe12345 Let me know if you're able to run it. If so, I will explain the line piece by piece. – user1717828 Oct 1 '15 at 13:56
  • yep . am able to run that but the value i get doesnt match the total if i run free -t – johndoe12345 Oct 1 '15 at 14:01
  • @johndoe12345 I added a note - the free memory will change each time you run the program (the computer is doing stuff, after all). Run free -t twice in a row really fast. It still changes, right? – user1717828 Oct 1 '15 at 14:02
  • yes i am aware of that but the numbers i get are very different so not matching up – johndoe12345 Oct 1 '15 at 14:06
  • and your $3 above is not free on all lines,in other words $3 on line w is 989180 when it should be 7300468 – johndoe12345 Oct 1 '15 at 14:07

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.