Display Memory Statistics With Free command on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

The Linux free command displays the total amount of used and free memory, swap memory (if enabled), and the cache/buffer used by the kernel. This information can be used by a system administrator to investigate whether there is a sudden spike in server resource consumption.

If you are planning to install any application or are going to deploy a new update, you can use this information to analyze the requirements and be prepared with additional resources as necessary.

In this guide, I’ll show you how to use the free command to print the amount of free and used memory in your system with various command-line examples.

Tutorial Details

DescriptionFree command
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisites–
Internet RequiredNo

Syntax of the free Command

The free command accepts two arguments: one for the option and the other for the argument, both of which are optional.

$ free [OPTION] [ARGS]

When you run this command without any options, it will give the following output:

free command output

Here, the output is divided into six columns for Mem/Swap, and the numbers are represented in bytes as follows:

  • total: This refers to the total amount of main memory and swap memory on your system.
  • used: The amount of memory used by running programs and processes.
  • free: The amount of memory that is not currently being used by any processes and is ready for allocation.
  • shared: The amount of memory used by tmpfs (temporary filesystem)
  • buff/cache: Sum of the memory used by buffers and cache.
  • available: Shows the total free memory to start a new program without swapping, which is β€œtotal – usedβ€œ.

The output of the free command can be tweaked using the different options it offers, which are as follows:

OptionDescription
-hPrint the information in human-readable formats, such as KB, GB, etc.
-b|k|m|gIt will only print the information in corresponding Bytes, KB, MB, and GB.
-s nUpdate the free command output to the defined β€œn” number of seconds.
-c nDisplay the stats β€œn” number of times.
-tAdd a third row that shows the sum of total β€œtotalβ€œ, β€œuserβ€œ, and β€œfree” memory.

Let’s now look at a command-line example of a free command with these available options.

1. Display information in human-readable format

When you run the free command without any options, the statistics in the output are represented in bytes, which may not be suitable or appealing for some; to address this, you can use the β€œ-h” option to print the information in a human-readable format.

$ free -h

Output:

print data in human-readable format in free command

2. Display the output in a specific unit only

There are a few additional β€œ-b|k|m|g” options that you can use to display the output in bytes, kilobytes, megabytes, and gigabytes. For example, the following command will show the free output in megabytes.

$ free -m

Output:

free command output in megabytes

3. Update the statistics continuously

When you execute the free command, the stats it shows are from the time of execution. To get real-time data like the htop command, use the β€œ-s n” option, where n is the number of seconds to wait for a refresh.

For example, the following command will refresh the output of the free command every 10 seconds.

$ free -s 10

Output:

refreshing data in free command

4. How many times to display the updated stats

The previous command repetitively updates the free command output every 10 seconds and will repeat endlessly until a user manually interrupts it with β€œCtrl+cβ€œ. However, you can use the β€œ-c n” option with the previous β€œ-s n” option to update the output only β€œn” times.

Let’s say you want to update the free command output every 10 seconds, repeating it 3 times. You can use the following command:.

$ free -s 10 -c 3

Output:

repitited free command only n times

5. Display the sum of physical and swap memory

By default, the free command shows the main memory and swap memory information, but if you want to get the sum of them, use the β€œ-t” option.

$ free -t

Output:

sum of main and swap memory in free command

Final Word

The free command is useful for checking memory consumption on the system, but you should also be aware of other tools, such as the ps command, which helps to identify the process using the most memory, and learn about methods to clear buffer and cache memory in Linux.

If you have any questions or queries related to the article, then let us know in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.