Random Access Memory (RAM) and Swap space are two of the most critical components of a Linux computer that you must monitor closely for better system performance. System administrators must check if there is enough RAM memory to install and start new applications. Linux provides the free command to check detailed information on the amount of memory usage.
In this tutorial, we will learn how to use the free command in Linux to display the system’s memory usage.
Linux Free Command
The free command in Linux displays the total amount of free and used amount of memory, including physical and swap memory on your system. Free retrieves the memory information from the
/proc/meminfo file. Free has evolved over time and you might see the different outputs in different versions of your operating system, except for the key parameters.
You must understand the free command headers to interpret the information correctly. You can use the free command options to change the output format for your better understanding.
Free Command Syntax
The basic syntax of the free command is:
Linux Free Command Output
The default output of the free command is in KiB format.
The Line 1 shows headers in 6 columns. The second line shows the memory (RAM) statistics and the following line shows the swap information ( in 3 columns).
When the free command is used without any options, the free command shows the amount of memory in kibibyte format. 1 kibibyte (KiB) equals 1024 bytes.
Now let’s understand what these separate columns (headers) mean:
- total - displays the total amount of memory installed on the system and total swap allocated during installation. Here, the total physical amount of memory is 4020192 KiB which is almost 3925 MiB and 4 GiB.
- used - displays the total amount of memory that is currently in use by your system and running applications. Here, total used memory is 757028 KiB which is almost 740 MiB and .72 GiB.
- free - displays free amount of memory that can be used by your system and other applications. Here, free memory is 1933808 KiB which is almost 1888 MiB and 1.84 GiB.
- shared - displays the shared amount of memory that is used by multiple processes and applications. Here, the total shared memory is almost 14 MiB.
- buff/cache - displays buffered (memory used by Kernel buffers) and cached (memory used by page cache) memory sizes that is not currently in use, but is available for later use.
- available - displays available memory size that is usable by new applications, but this size does not include swap memory. Here, the available memory is almost 2946 MiB and 2.87 GiB.
For swap memory, only the total, used, and free amount of memory is displayed.
What is Swap in Free Command
Swap in Linux is used when total physical memory is used. When entire RAM is used and your system needs more resources, then inactive memory pages are moved to swap. Swap can help systems with lower physical memory, but it is not a replacement for RAM. Swap is located in the hard drive and thus it is considerably slower than the physical memory.
Free Command Examples
The free command offers various options to check how much memory is available, free, and in use. The following examples show you how to use free with these options to represent output in an easy to interpret format.
To display the memory sizes in mebibyte (MiB) format, use the
1 MiB equals 1024 KiB.
-h option to display memory usage in human-readable format:
Unlike other unit options like -m for MiB, the human-readable output format is not displayed in a single unit. It is displayed in the highest available unit.
-b option to display output in bytes:
This output may not be easy to understand for the users, but it may be required for some applications or programs.
The following command displays the memory information in the KiB format with the
This is also the default output of the free command in Ubuntu and several other Linux distributions.
-g option to display free command output in the GiB format:
The output is always rounded off to display only the whole numbers.
To display sum of both swap and physical memory, use the
-t option. Additionally, it shows the total used and free space. You may see the last line showing column totals.
To continuously monitor the free and used memory on your Linux system, you can use the
-s option and specify the number of seconds after which the command will repeat the output:
free -s 2
Here the first instance shows used memory 724260, but after we started a new application, the used memory number kept changing. Use Ctrl + c to return to the command prompt.
In this free command example, 2 denotes the number of seconds after which the command will repeat. You can also specify the number of times to repeat the command:
free -c 3
In this case, the command will repeat only 3 times and after that, it will return to the command prompt.
By default, the kernel buffer and page cache memory numbers are combined in the output, because they interact and depend on each other. Use the
-w option to display cache and buffers output separately:
Free Command Options
The free command provides multiple options to display output in different formats and to control the command behavior. The following table describes some of the most commonly used free command options:
|Display memory usage information in bytes.|
|Display memory usage information in kibibytes (KiB).|
|Display memory usage information in mebibytes (MiB).|
|Display memory usage information in gibibytes (GiB).|
|Display memory usage information in a human-readable format. The output is displayed in the highest possible units, for example, 1025 MiB will be displayed as 1 GiB and not as 1025 MiB.|
|Update the output for the number of specified times. -c 3 will refresh the output 3 times.|
|Update the output continuously after the specified number of seconds. -s 2 will refresh the output every 2 seconds.|
|Display buffers and cache memory usage information in separate columns.|
|Display a sum of the total, used, and free memory for both physical memory and swap.|
|Display detailed low and high memory statistics.|
You can combine these options to modify the output based on your requirements.
In this tutorial, we learned about the free command in Linux. The free command displays usage information for physical memory and swap. This command is available in all modern Linux distributions and is very simple to use.