How to Use Command free In Linux System

In Linux operating systems, the free command displays the total amount of free and used physical and swap memory, and the buffers used by the kernel. you can use the free command to get a detailed report on the system’s memory usage.

How to Use the free Command

The syntax for the free command is as follows:

free [OPTIONS]

When used without any option, the free command will display information about the memory and swap in kibibyte. 1 byte (KiB) is 1024 bytes.

free

The output will include three lines, a header, one line for the memory and one for the swap:

              total        used        free      shared  buff/cache   available
Mem:        8075208     3204964     1310540      551232     3559704     4198340
Swap:       2097148           0     2097148

Here’s what each column mean:

  • total – This number represents the total amount of memory that can be used by the applications.
  • used – Used memory. It is calculated as: used = total - free - buffers - cache
  • free – Free / Unused memory.
  • shared – This column can be ignored as it has no meaning. It is here only for backward compatibility.
  • buff/cache – The combined memory used by the kernel buffers and page cache and slabs. /this memory can be reclaimed at any time if needed by the applications. If you want buffers and cache to be displayed in two separate columns use the -woption.
  • available – An estimate of the amount of memory that is available for starting new applications, without swapping.

Showing Memory Usage in Human Readable Format

As we saw in the example above, by default, the free command shows the memory information in kibibyte. To view the information in human-readable format (usually M and G), use the -h option:

free -h

Output:

<code">              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 Metrics

The free command also allows you to specify the unit in which the memory is measured. 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.

For example, to show the output in megabytes you would type:

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

To display a line showing the column totals use the -t option. This will give you a sum of the memory and swap in the total, used and free columns.

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

To continuously display the memory information on the screen use the -s--secondsoptions followed by a number that specifies the delay.

For example, to print the memory information every five seconds you would run:

free -s 5

The free command will continue to display the result until you press CTRL+C. This is similar to the behavior of the watch command.

To display the result for a specific number of times use the -c--count option. In the example below the command will print the result ten times:

free -s 5 -c 10

Conclusion

By now you should have a good understanding of how to use and interpret the free command. To view all available free command line options type man free in your terminal.

Leave a Reply

Your email address will not be published. Required fields are marked *