Services Spotlight:

Understanding the free command in Ubuntu (and Linux)

Written By Corlew Solutions
Updated February 5, 2014
Published February 5, 2014
Why Corlew Solutions?

We write great software and provide amazing technical support. Contact us today if you need help with a software project:

  • Website: Send an inquiry
  • Email:
  • Phone: (703) 688-3058

Article Technology Info

This article discusses the following technologies:

  • free (Ubuntu Command) -
    (docs)
  • Ubuntu - A Debian-based Linux operating system that is a popular server choice.
    (website | download | wiki)

The free command in Linux based operating systems like Ubuntu is a powerful tool for viewing memory allocation. It’s one of those tools that server administrators should understand well, but (like many Linux tools) can be confusing. This article explains the free command output and some typical ways you might want to use free. Let’s dive in!

Free Basics

Free is dirt simple to run:

1
free

Running the above command will generate output similar to the table below:

Some simple facts about this output include:

  • All numbers are in Kilobytes (KB) (i.e. 1024 bytes) by default, but this can be customized (see below).
  • The shared column is obsolete. (This is noted on the free man page).
  • The output shows memory sizes and allocation for RAM and swap memory, but it also explains how much of that physical RAM is allocated for buffering and caching when it IS NOT being used by an application.

Free has a couple useful options if you would like to view the output in other units:

1
2
3
4
5
6
7
8
free -b   #Displays output in Bytes
free -k   #Displays output in Kilobytes
free -m  #Displays output in Megabytes
free -g   #Displays output in Gigabytes
free --tera  #Displays output in Terabytes
free -h   #Displays output in a human readable format (e.g. 1.5M)

free -t   #Add s a total row to the output

By default, free uses 1024 bytes when it converts between units. This is typically the standard when talking about memory or disk size, but it may lead to unexpected amounts if you’re comparing to numbers based on 1000 (i.e. 1KB = 1000 Bytes). You can tell free to convert with 1000 by adding the –si when converting.

1
free --si -m   #If a conversion option like -m, -b, etc.. is not specified, then the --si option is ignored

Understanding the Free Command Memory Output

Free’s output, it can be confusing at first. Make sure this fact sinks-in as deep as possible: Linux will take unused memory (a.k.a memory not being used by applications and the OS) and use it for disk buffering and caching WHEN IT CAN.

As far as Linux is concerned there’s no point in letting this free memory go to waste doing nothing, so it is put to work until an application needs it. If an application comes along and requests part of the memory being used for buffering and caching then Linux will provision it to the application. This process is transparent and never needs to be disabled (if there is a way).

With this is mind, let’s demystify us some free output. Executing free -m generates the following allocation amounts in Megabytes:

In our example, the yellow box holds the Total Physical RAM on the system. Simple enough.

Where most people get tripped up is trying to determine used vs free memory. They mistakenly look at the blue box and determine their used amount. Then they scan to the right and see a very small number where the red box is and determine their free memory. Then they start to get an uneasy feeling in their stomach because the number is so small which leads them to this page. Fortunately their gut is right; they are ready it wrong.

As far as Linux is concerned, the memory temporarily being used for buffering and caching is being used, so it is included in the Total Used number in the blue box. What is needed is a way to group the memory allocated for buffering and caching with the free memory instead. The orange boxes hold these amounts.

How Much Memory Is Really Being Used In Linux?

When we’re interested in server memory allocation, we don’t care about how much memory is being used for buffering and caching. We need to know what is being used by applications and what is available to applications. Row 2 of the output solves this problem.

The purple box represents what has been assigned to applications and the OS. It can easily be computed/verified by subtracting the two orange boxes from the blue box . This is the Real Used Memory on the system.

How Much Memory Is Really Free In Linux?

The Real Free Memory amount is contained in the green box. This is computed by subtracting the purple box from the yellow box. You can also arrive at the value by adding the red box and two orange boxes together.

Note About Free Rounding Errors

If you’re trying the calculations mentioned above, you may notice that free is off by a Megabyte. Free appears to truncate the memory amounts before they get reported. Using free in Kilobyte mode will produce more accurate results.

What About Swap Memory?

Swap memory is just a part of the hard disk that has been allocated to act like RAM. It would be pointless to use the disk for buffering and caching so Linux doesn’t use it and free only shows the actual memory being used on the disk. Returning to our example:

7 Megabytes are currently being used on the disk as memory. This is probably space that has been reserved by the OS for accounting purposes to track the rest of the Swap memory. You NEVER want to see swap memory used. It’s too slow for a server. If the system is using swap memory something is likely wrong or there is not enough physical memory on the machine. When it comes to swap memory, you’ll mainly use free to verify that Linux is not using swap memory.

Tips for Using The Linux Free Command

Tip 1: Monitor Memory Changes Over Time

Instead of repeatedly typing free into the terminal, you can use one of these two commands to periodically run free.

1
2
free -s  # Leaves old values on screen until they scroll off
watch -n <interval in seconds> free  #Overwrites old values

If you have any more tips or interesting uses for the free command we’d love to hear about them in the comments.

We hope you found this article useful. If you see any mistakes, missing features or ways to improve it, please let us know in the comments below so we can update its contents. If you're willing to link to us, we would sincerely appreciate it!

References

Corlew Solutions is a Web Design and Web Application Development company based in Fairfax Virginia, minutes away Washington D.C. If you're looking for great web design from the Northern Virginia area or web design from the Washington D.C. area we can help. Contact Us today!

comments powered by Disqus