Free Command in Linux Explained With Examples
Commands

Free Command in Linux Explained With Examples

Christopher Murray
Christopher Murray

Explaining the output of the Free command in Linux

Using Free Command in Linux is pretty simple. Here’s the syntax for the free command:

free [options]

A sample output for the free command may look like this:

[chris@machine ~]$ free  
              total       used        free      shared       buff/cache    available  
Mem:        8048372     2593004     1366712   658380       4088656      4494976
Swap:             0           0           0

The free command without options returns results for ‘total’, ‘used’, and ‘free’ memory on your system by accessing information from the Linux kernel. It also displays categories for ‘shared’, ‘buff/cache’, and ‘available’.

To avoid some potential confusion, let’s clarify what those terms mean.

  • Total is straightforward. This figure represents all existing memory.
  • Used is a calculation of the total system ram minus allocated free, shared, buffer, and cache memory.
  • Free is memory that is not being used for any purpose.
  • Shared, Buffer, and Cache fields identify memory being used for kernel/operating system needs. The buffer and cache are added together and the sum is listed under ‘buff/cache’.
  • Available memory appears in newer versions of free and is intended to give the end user an estimation of how many memory resources are still open for use.

These clarifications are important. Incorrectly attributing meaning to the terms free or used memory can create a misconception of your system’s memory use.

This may lead an inexperienced user to falsely believe their system needs to be upgraded with more RAM. Note that in previous versions there was no display for available memory. Users may incorrectly assume that because there is high memory usage, their hardware is underpowered. The available tab was presumably put in place to help offset this common misunderstanding.

The Linux operating system uses caching to improve performance. In very basic terms, this means that a certain amount of memory is set aside for use before it’s needed so it can be processed more quickly. This is a standard process and nothing to be concerned about unless the values seem very unusual for your current use.

The ‘available’ memory estimate is probably adequate for someone who simply wants to know how their system is responding to certain applications. If you are unable to see this field, you may need to update to the latest version of ‘free’. You can check your current version by running ‘free -V’. My version 3.3.15 has the ‘available’ in its output.

Customizing the output of the free command

The default output displays information in kilo bytes, but there are options to display in different formats if you prefer. Running the help (free -help) displays all the possible options you can append.

The most useful option here in my opinion is -h which shows the output of the free command in human readable format.

[chris@machine ~]$ free -h
           total     used     free   shared  buff/cache   available
Mem:       7.7Gi    2.5Gi    1.5Gi    479Mi    3.7Gi    4.4Gi
Swap:         0B       0B       0B

There are other options. They show the same requested information with some basic math conversions. I think that the human readable option (free -h) is one of the most effective for an everyday user.

Automate the free command and run it continuously or periodically

Another great feature is the capability to automate the command. There are two options that help us customize this tool:

  • There is ‘-s’, which runs the free command for the designated interval of seconds until the user quits the program (^+C).
  • There is also ‘-c’ which can be used separately or in conjunction with the seconds option. If you enter only ‘-c’ and an integer (n), it will run the command n number of times. By default it uses one second intervals.

Let’s say you want to open a series of applications and see how your memory is affected. For my test output I will use the human readable format using powers of 1000 (Gb) instead of 1024 (GiB). I’m going to record for 20 seconds to analyze the impact. I will capture the data every 5 seconds and I will do this for 4 counts. Here is this example formatted for the command line and its output:

[chris@machine ~]$ free -h --si -s 5 -c 4
           total     used     free   shared  buff/cache   available
Mem:        7.9G     2.8G     1.2G     501M     3.8G     4.2G
Swap:         0B       0B       0B
           total     used     free   shared  buff/cache   available
Mem:        7.9G     2.8G     1.2G     501M     3.8G     4.2G
Swap:         0B       0B       0B
           total     used     free   shared  buff/cache   available
Mem:        7.9G     2.9G     1.1G     549M     3.9G     4.1G
Swap:         0B       0B       0B
           total     used     free   shared  buff/cache   available
Mem:        7.9G     3.0G     998M     553M     3.9G     4.0G
Swap:         0B       0B       0B

I waited a few seconds and then I opened a few browser tabs and accessed some bookmarks. The stress of those activities is noted by the fluctuations in the output. Please note, the effect would be more pronounced using an output format with less rounding. For our purely demonstrative purposes, this is unnecessary.

If you want to continually monitor the memory usage with free command but don’t want its output to clutter the screen, you can use the watch command together with free command.

This will show you just one output for the free command but this output will be changed at regular interval.

watch free -h

Conclusion

This tutorial demonstrated how to get started using the ‘free’ command in Linux. Hopefully, you find this guide helpful and easy to understand. ‘Free’ can be used to analyze system memory usage and can be tweaked using its various options to finely tailor output for your needs.

If you have any questions or suggestions, please let us know in the comment section.



Join the conversation.