The “free” Command in Linux [8 Practical Examples]

The free command in Linux is a handy tool for system administrators and other users as well. It provides valuable information about the available and used memory in the system. In this article, I will describe the free command in Linux and its various options, with practical examples that demonstrate its usage.

A. Description

The free command in Linux is a useful tool that outputs the summary of RAM availability and usage, including the amount of physical memory, swap memory, and buffers. The command facilitates the monitoring of resource utilization and enables administrators to assess if sufficient space is available to run additional applications.

B. Syntax

The syntax of the free command in Linux is simple, as shown below.

free [OPTION]...
Note: In the syntax above, the OPTION enclosed by the square brackets means it is not mandatory and three dots after the square brackets mean multiple OPTIONS can be used after the free command.

C. Options

The free command has multiple options available. I have listed a few of them. You can learn more about the free command, its options and their uses by checking the man page.

man free

Useful Options

  • -b, –bytes: Prints the amount of memory in bytes.
  • -k, –kibi: Prints the amount of memory in kibibytes. This is the default.
  • -m, –mebi: Prints the amount of memory in mebibytes.
  • -g, –gibi: Prints the amount of memory in gibibytes.
  • –tebi: Prints the amount of memory in tebibytes.
  • –pebi: Prints the amount of memory in pebibytes.
  • –kilo: Prints the amount of memory in kilobytes.
  • –mega: Prints the amount of memory in megabytes.
  • –giga: Prints the amount of memory in gigabytes.
  • –tera: Prints the amount of memory in terabytes.
  • –peta: Prints the amount of memory in petabytes.
  • -h, –human: Prints the memory in a human-readable format.
  • -w, –wide: Switch to Wide mode, which generates lines exceeding 80 characters.
  • -c, –count “X”: Displays the result X times.  Requires the -s option.
  • -s, –seconds “X”: Continuously prints result after every ‘X’ seconds.
  • –si: Uses kilo, mega, giga etc (power of 1000) instead of kibi, mebi, gibi (power of 1024).
  • -t, –total: Prints the total amount of free and used physical memories in kilobytes.
  • -V,–version: Displays the version of the free command.
  • –help: Displays the help information of free command.
Note: Commands and their options are case-sensitive in Linux. So, be careful while using them.

Practical Examples of the “free” Command in Linux

In Linux, the free command is a helpful tool to get information about the available and used memory in the system. In the section below, I will show you some of the most useful applications for the free command in Linux.

Example 1: Output Memory Usage in Human Readable Format

A useful way to keep track of your system’s memory usage is to output memory usage in a human-readable format. Whether you are a system administrator trying to troubleshoot performance issues or you are trying to optimize your code, understanding the memory usage of your system is important. In a Linux environment, the “free” command is commonly used to display information about total memory, usage and the available amount of memory, all in kilobytes. However, this output can be difficult for a human to interpret. So, you can follow the steps below to get the output in a human-readable format using option -h with the free command.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Now, execute the following command.

free -h

➌ Then, press the ENTER key.

Output >

In the following image, you can see that I have displayed the memory amount in a human-readable format.Showing the memory amount in a human-readable format using the free command in linux.


Similar Readings


Example 2: Continuously Show Memory Usage After a Specific Time

You can monitor the amount of memory usage after a specific time using the free command combined with the option -s. The option -s of the free command generally sets the interval between updates in seconds. In this section, I will demonstrate the use of the -s option as well as its output. You can do the same by following the steps given below.

Steps to Follow >

➊ First, open the Ubuntu Terminal.

 Next, run the command to continuously display the memory usage in a time interval of “5” seconds.

free -s 5

➌ Finally, tap the ENTER button.

Output >

As you can see in the image below, I have continuously displayed the memory status over a 5-seconds time span.The memory status has been continuously displayed over a 5-seconds time span using the free command in linux.

Note: The “-s” option of the “free” command only allows you to set the update time interval between outputs, not the number of times the output should be displayed.

Example 3: Specify Output Units of “free” Command in Linux

The free command in Linux allows you to specify the units of memory usage. There are different units like bytes, kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, kilobytes, megabytes, etc. The options for each unit are mentioned in the “Useful Options” section. In this example, I will display the memory status in mebibytes (using option -m, –mebi) and megabytes (using option –mega) units. However, the –mega option gives the same output as that of the combined options –si -m or –si –mebi. You can use the options to display the memory in the following manner.

Note: The option –si stands for International System for Units. Option –mebi gives an output in mebibytes (1024 KiB or MiB), which is a unit of digital information storage. When options –si and –mebi merge together, the output is shown in SI units (1000 KB). That’s why the options –mega and –si–mebi give the same result.

Steps to Follow >

➊ Open the Ubuntu Terminal.

 Execute the following command to display the memory usage in the mebibytes unit.

free -m

OR,

free --mebi

➌ Then, press the ENTER key.

➍ Now, run the following command to display the memory usage in megabytes.

free --mega

➎ At this point, hit the ENTER button.

➏ You can get the exact same result as “free –mega” by executing the command below.

free --si -m

OR,

free --si --mebi

➐ In the end, tap the ENTER button.

Output >

In the following image, you can see that I have displayed the memory usage in different units using the free command.The amount memory usage is shown in different units.


Similar Readings


Example 4: Print Output Memory Details Multiple Times Using the “free” Command in Linux

The “free” command provides an easy way to check memory usage, and with the “-c” option, you can print the output multiple times. The “-c” option sets the number of updates, making it easy to monitor memory usage over time. In this example, I will print out the memory details three times in the terminal. You can do the same by following the steps below.

Steps to Follow >

➊ Open the Ubuntu Terminal.

➋ Then, execute the below command.

free -c 3

➌ Press the ENTER button.

Output >

The below image shows that I have printed out the memory details three times in the terminal.The memory details is printed three times in the terminal.

Example 5: Separate Buff and Cache Columns Using the “free” Command in Linux

The “free” command with the “-w” option in Linux provides a useful way to separate buffers and cache memory and monitor memory usage more effectively. By separating these columns, you can obtain a clearer picture of memory usage and make decisions about performance optimization and troubleshooting. In this example, I will show you the process to separate the “buffers” and “cache” columns. You can do the same by following the steps below.

Steps to Follow >

➊ First, open the Ubuntu Terminal.

➋ Then, execute the following command.

free -w

➌After that, hit the ENTER key again.

Output >

The image below displays the separation of the “buffers” and “cache” columns while displaying the memory details.The “buffers” and “cache” columns are separated while displaying the memory details using the free command in linux.


Similar Readings


Example 6: Displaying the Total Memory Column Using the “free” Command in Linux

The “free” command with the “-t” option in Linux provides a useful way to display the total memory. By including the total memory in the output, you can obtain a clearer picture of memory usage. Here, in this section, I will show you how to display the “total” column, which displays the amount of total memory usage. You can do the same by following the steps below.

Steps to Follow >

➊ Open the Ubuntu Terminal.

 Then, execute the command below.

free -h -t

OR,

free -h --total

➌ Now, tap the ENTER button.

Output >

In the following image, you can see that I have displayed the total memory column using the “free” command in Linux.The total memory column has been displayed using the “free” command in Linux.

Example 7: Get Help About the “free” Command in Linux

In Linux, the help page for the free command contains a brief description of the command. You can see the help page by following the steps mentioned below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Then, type the following command into the command prompt:

free --help

➌ Now, press the ENTER button.

Output >

In the image below, you can see the help page for the free command in Linux.Showing the help page for the free command in Linux.


Similar Readings


Example 8: Get the Version of the “free” Command in Linux

You can see the current version of the free command on your Linux machine with the –version option. Here, I will show you the version of the free command for my machine on the terminal. You can do the same by following the steps given below:

Steps to Follow >

➊ Open the Ubuntu Terminal.

 Run the following command:

free --version

➌ Press the ENTER key.

Output >

In the following image, you can see that I have displayed the version of the free command in Linux.The version of the free command in Linux has been shown.

Conclusion

The free command in Linux is a simple and powerful tool for monitoring memory usage on the system. By utilizing its various options, you can gain a complete understanding of your system’s memory utilization and make decisions about optimizing memory usage. I have designed this article aiming to provide a comprehensive guide to the free command, with practical examples to help you get started and become an advanced user.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

Leave a Comment