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 is dirt simple to run:
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.
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, theholds 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 theand determine their used amount. Then they scan to the right and see a very small number where the 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. What is needed is a way to group the memory allocated for buffering and caching with the free memory instead. The 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.
Therepresents what has been assigned to applications and the OS. It can easily be computed/verified by subtracting the two from the . 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. This is computed by subtracting the from the . You can also arrive at the value by adding the and two 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.
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!