Using the Linux free Command

December 29, 2021

Introduction

The Linux free command outputs a summary of RAM usage, including total, used, free, shared, and available memory and swap space. The command helps monitor resource usage and allows an admin to determine if there's enough room for running new programs.

In this tutorial, you will learn to use the free command in Linux.

Using the free command in Linux.

Prerequisites

  • A system running Linux
  • Access to a terminal (Ctrl+Alt+T)

Note: The free command is a great way to monitor RAM usage on a server. However, the best way to secure a smooth server operation is to secure sufficient resources for running all apps. Choose one of our pre-configured Bare Metal Cloud server instances that best suits your needs and never run out of resources.

free Command Syntax

The free command takes the following syntax:

free [options]

[options] are discussed in the following section, and they are optional.

Running the free command without options outputs information about memory and swap, expressed in kibibytes:

An example output for the free command in Linux.

Each column depicts the following:

ColumnDescription
totalTotal RAM amount available on the system.
usedMemory currently in use by processes.
freeUnused memory, free.
sharedMemory shared by multiple processes.
buff/cacheMemory in use by the kernel (for buffers, page cache, and slabs).
availableEstimated memory amount available for starting new applications, excluding swap.

free gathers information by parsing the /proc/meminfo file.

free Command Options

Use options to customize and format the output. The free command accepts the following options:

OptionDescription
-b, --bytesDisplays the memory amounts in bytes.
-k, --kibiDisplays the memory amounts in kibibytes. The command uses kibibytes by default.
-m, --mebiDisplays the memory amounts in mebibytes.
-g, --gibiDisplays the memory amounts in gibibytes.
--tebiDisplays the memory amounts in tebibytes.
--pebiDisplays the memory amounts in pebibytes.
-h, --humanScales and formats the output to the shortest three-digit unit to produce a human-readable output.
-c, --count [count]Updates the output [count] times.
-l, --lohiShow detailed low and high memory statistics.
-s, --seconds [delay]Continuously updates the output every [delay] seconds.
--siUse power of 1000 (KB, MB, GB, etc.) instead of power of 1024 (KiB, MiB, GiB, etc.). For example, use --mebi --si to format the output in megabytes. Alternatively, use --kilo, --mega, --giga, and --tera.
-t, --totalDisplays a line showing the column totals.
--helpPrints the help file.
-V, --versionDisplays the program version.

free Command Examples

The following section shows different ways of using the free command.

Example 1: Output Memory Usage in Human Readable Format

The free command displays memory sizes in bytes by default, which isn't practical in modern systems with large amounts of RAM. Make the free command output easier to read by specifying the -h option:

free -h
Using the -h option to produce a human-readable output of the free command.

The command formats the output using the most appropriate unit for each value - KiB, MiB, GiB, etc.

Note: Limit the amount of system resources a user can consume with the ulimit command.

Example 2: Continuously Show Memory Usage

Use the -s option to continually refresh the free command output and monitor an app's resource usage. Invoke the free command with the -s option and specify the delay between each output refresh:

free -s2
Continuously reporting memory usage in Linux with the free command.

The example above instructs free to refresh the output every two seconds. Stop the process with Ctrl+C.

Example 3: Specify Output Units

Specify a unit for displaying the memory values uniformly. Use powers of 1024 (the default) or 1000.

1. For powers of 1024, use:

  • -b, --bytes
  • -k, --kibi
  • -m, --mebi
  • -g, --gibi
  • --tebi
  • --pebi

For example:

free -m
Specify units to use for the free command output.

The command produces an output with values expressed in mebibytes.

2. For powers of 1000, use:

  • --kilo
  • --mega
  • --giga
  • --tera
  • --peta

For example:

free --mega
Use megabytes for expressing values in free command output.

The output values are expressed in megabytes. Alternatively, use the --si option followed by the corresponding power of 1024 unit:

free --si --m
Use the --si option for expressing values in megabytes.

The output values are expressed in megabytes.

Example 4: Print Output Multiple Times

Specify the -c option to instruct free to automatically quit after refreshing the output a specific number of times. In the following example, the command prints the result four times:

free -c 4
Instruct free to print an output multiple times.

After four updates, the command automatically quits.

Note: Run any command in regular intervals with the Linux watch command.

Example 5: Separate Buff and Cache Columns

The buff and cache areas interact and depend on each other, so they are combined in the output. To see the buffers and cache columns separately, specify the -w option:

free -w
Generate a wide output, separating the buffers and cache columns in the free command output.

The output now shows a buffers column and a cache column separately.

Example 6: Show the Total Memory Column

The --total option instructs free to print a Total line that sums the values from the total, used, and free columns of the Mem and Swap lines.

free -h --total
Show the Total line in free command output.

In the example above we instructed free to show the Total line and produce the output in a human-readable format.

Example 7: Combine Options

Combine different free command options to automate and facilitate memory monitoring in a system. For example, combine the -s and -c options to make free produce an output several times with a delay between refreshes:

free -m -s3 -c4
Combining different options in the free command.

The command runs four times with a three-second delay between updates and expresses the values in mebibytes.

Conclusion

This tutorial showed how to use the free command to monitor memory usage on a Linux system. The command allows system administrators to monitor RAM usage on a system or server. Therefore, it is a helpful utility for identifying a potential issue. Learn more about system memory by reading our article on the differences between Intel Optane, RAM, and SSD.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
CPU Vs. GPU: A Comprehensive Overview
December 9, 2021

The hardware components in charge of providing the computing power evolved in response to the high demand. This article will provide a comprehensive comparison between the two main computing engines - the CPU and the GPU.
Read more
How to Use the top Command in Linux
December 2, 2021

The top command is a built-in Linux utility that contains useful information about your system's running processes and resource usage. Learn how to use the top command effectively.
Read more
How to List Running Processes in Linux
December 29, 2021

Tracking and managing running processes is an important part of Linux administration. This tutorial shows different methods you can use to list running processes in Linux.
Read more
How to Use the vmstat Command
March 18, 2021

The vmstat command is a Linux monitoring utility used to see various system statistics and to diagnose performance bottleneck issues. Learn how to use the command in this simple tutorial with examples for each available option.
Read more