Free Command in Linux

Free Command in Linux

Before we start discussing free command in Linux, let's first understand-What is a free command ?

The free command in Linux is used to retrieve information about the system's memory usage. It provides insights into total, used, and available memory, as well as swap usage. Understanding the free command can assist in monitoring and managing system memory effectively.

In this tutorial, we will talk about the free command that reports the total physical and swap space, as well as free and used space. We will also address a few FAQs on free command in Linux.

How to use the free Command

The syntax of the free command is as follows:

free [OPTIONS]

When used without options, the free command displays memory and swap information in kibibytes. One kibibyte (KiB) is 1024 bytes.

free

The output contains 3 lines, a header, a line for memory, and a line for swap.

Output

              total        used        free      shared  buff/cache   available
Mem:        8075208     3204964     1310540      551232     3559704     4198340
Swap:       2097148           0     2097148
ℹ️
The output could be a little bit different in earlier Linux versions.

The meaning of each column is as follows:

  • total - This number represents the total amount of memory available to the application.
  • used - Memory used. It is calculated as follows - used = total - free - buffers - cache.
  • free - Free / Unused memory.                                                                                        
  • shared - This column is meaningless and can be ignored. Used here only for backward compatibility.
  • buff/cache - The total amount of memory consumed by slabs, page cache, and kernel buffers. In the event that the applications require it, this RAM can always be reclaimed. Use the -w option if you wish buffers and cache to be presented in separate columns.
  • available - Estimate the amount of memory available to launch a new application without swapping.

Showing Memory Usage in Human Readable Format

By default, the free command displays memory information in kibibytes. Use the -h option to display information in human-readable format (typically megabytes and gigabytes).

free -h
Output

               total        used        free      shared  buff/cache   available
Mem:           487M        219M         54M        4.5M        214M        228M
Swap:          1.5G          0B        1.5G

Showing Memory Usage in Other Metric

You can also use the free command to specify the unit for measuring memory.

The valid options are:

  • -b, --bytes - Display output in bytes.
  • --kilo - Display output in kilobytes (1KB = 1000bytes).
  • --mega - Display output in megabytes.
  • --giga - Display output in gigabytes.
  • --tera - Display output in terabytes.
  • -k, --kibi - Display output in kibibytes. (1KiB = 1024bytes). This is the default unit.
  • -m, --mebi - Display output in mebibytes.
  • -g, --gibi - Display output in gibytes.
  • --tebi - Display output in tebibytes.
  • --peti - Display output in pebibytes.
  • --si - Instead of 1024, use powers of 1000. For example --mebi --si is equal to --mega.
free --mega
Output

               total        used        free      shared  buff/cache   available
Mem:           8075        4022         233         614        3819        3336
Swap:          2097           0        2097

Showing the Column Totals

Use the -t option to display a line with the column totals. You can see the total, used, and free RAM and swap columns after doing this.

free -h -t
Output

               total        used        free      shared  buff/cache   available
Mem:           7.7G        3.9G        483M        526M        3.4G        3.2G
Swap:          2.0G          0B        2.0G
Total:         9.7G        3.9G        2.5G

Continuously Print the Output

When free is run with the -s (--seconds) option and a delay value specified, the memory information will be displayed on the screen continually.

For instance, you would execute: to report the RAM information every five seconds.

free -s 5

Until you press CTRL+C, the free command will continue to display the outcome. This is comparable to how the watch command behaves.

Use the -c (--count) option to specify how many times the result should be displayed. The command will print the outcome ten times in the example below:

free -s 5 -c 10

FAQs on Free Command in Linux

How do I use the free command? 

Simply open a terminal and type free followed by pressing Enter. The command will display the memory information for your system.

What are the units used in the memory information provided by free? 

By default, the free command displays memory information in kilobytes (KB). However, you can use options like -h or --human-readable to display memory sizes in human-readable units, such as megabytes (MB) or gigabytes (GB).

How can I view the free command output with updated values at regular intervals? 

You can use the -s or --seconds option followed by a time interval in seconds to refresh the free command output continuously.

What do the columns labeled "+/- buffers/cache" represent in the free command output? 

The "+/- buffers/cache" columns represent the actual memory used and free memory, excluding buffers and cache. It helps to gauge the available memory for applications.

Can I use the free command to monitor swap memory? 

Yes, the free command output includes information about swap memory, such as total, used, and free swap memory size.

Can I specify the memory units in the free command output? 

Yes, you can use the -k, -m, -g, or --si options to specify the memory units as kilobytes, megabytes, gigabytes, or in normal SI units respectively.

How can I view detailed information about the free command options and their usage? 

To view detailed information about the free command options and their usage, refer to the man pages. Open a terminal and type man free.

Conclusion

The free command is an essential tool for monitoring system memory usage in Linux. By using various options and interpreting the output correctly, you can effectively manage memory resources and troubleshoot memory-related issues.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.