Skip to content

Free

by mat on May 17th, 2009

Using the free command gives output something like this (-m just tells free to output in megabytes):

free -m
total used free shared buffers cached
Mem: 2013 1996 17 0 27 1381
-/+ buffers/cache: 588 1425
Swap: 956 0 956

This is useful but annoying as it doesn’t show you how much memory is actually free. Files that are used are kept in the ram (referred to as caching). If the memory is needed it is simply overwritten, however if the file is used again it is already in the memory which saves time.

I wrote a quick little bash line to get the actually memory in use or the actual memory free. I use grep to select the right line from the output of free, and then use awk to do the string manipulation and math.

Memory in use

free -m | grep Mem | awk ‘{x=$3-$7; print x}’

Memory free

free -m | grep Mem | awk ‘{x=$2-($3-$7); print x}’

One Comment
  1. Kevin M permalink

    The “grep” process can be avoided by using awk’s pattern match

    i.e
    $ free -m | awk ‘/^Mem:/ {x=$2-($3-$7); print x}’

Leave a Reply

Note: I am currently writing my thesis so probably wont have time to reply to your comment
Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS