2024-09-24
The simplest form of the command, free
, provides a snapshot of your system’s memory usage. Let’s break down a typical output:
total used free shared buff/cache available
Mem: 1996 162 1677 11 157 1786
Swap: 2047 0 2047
The free
command offers many useful options to customize the output:
-h
(or --human-readable
): Displays the output in a more human-friendly format, using units like KB, MB, and GB. This is highly recommended for easier interpretation.free -h
-m
(or --mega
): Displays the output in megabytes.free -m
-g
(or --giga
): Displays the output in gigabytes.free -g
-b
(or --bytes
): Displays the output in bytes.
-s <interval>
(or --interval <interval>
): Displays the memory usage information repeatedly at the specified interval (in seconds). This is very useful for real-time monitoring.
free -h -s 2 # Display memory usage every 2 seconds.
-t
(or --total
): Displays the total memory usage (sum of Mem and Swap).free -h -t
-o
(or --only-mem
): Shows only the memory information without the Swap information.free -h -o
-w
(or --width <width>
): Use this to set the output widthfree -h -w 60
High used
memory and low available
memory could indicate a memory leak, a process consuming excessive resources, or insufficient RAM. High Swap
usage could point to insufficient RAM, requiring you to either upgrade your RAM or optimize your processes to consume less memory. The free
command provides the foundational data to investigate these scenarios further. By combining free
with other commands like top
(for monitoring processes) and ps
(for listing processes), you can effectively diagnose and resolve memory-related issues.
The output of free
can be further processed using other commands like awk
to extract specific values or create custom reports. For example, to get just the available memory in megabytes:
free -m | awk '/Mem:/ {print $7}'
This example uses awk
to filter the output for the line containing “Mem:” and then print the 7th column (available memory in MB). Similar techniques can be used to extract and manipulate any other data field from the free
command’s output.