Friday, November 15, 2024
Google search engine
HomeGuest BlogsUsing the Linux free Command

Using the Linux free Command

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.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.An example output for the free command in Linux.

Each column depicts the following:

Column Description
total Total RAM amount available on the system.
used Memory currently in use by processes.
free Unused memory, free.
shared Memory shared by multiple processes.
buff/cache Memory in use by the kernel (for buffers, page cache, and slabs).
available Estimated 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:

Option Description
-b, --bytes Displays the memory amounts in bytes.
-k, --kibi Displays the memory amounts in kibibytes. The command uses kibibytes by default.
-m, --mebi Displays the memory amounts in mebibytes.
-g, --gibi Displays the memory amounts in gibibytes.
--tebi Displays the memory amounts in tebibytes.
--pebi Displays the memory amounts in pebibytes.
-h, --human Scales 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, --lohi Show detailed low and high memory statistics.
-s, --seconds [delay] Continuously updates the output every [delay] seconds.
--si Use 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, --total Displays a line showing the column totals.
--help Prints the help file.
-V, --version Displays 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.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.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.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.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.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.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.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.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.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

Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments