What it is
A command-line utility to display the amount of free and used memory in the system.
Installation
Linux
free is typically pre-installed on most Linux distributions. If not:
- Debian/Ubuntu:
sudo apt update && sudo apt install procps - Fedora/CentOS/RHEL:
sudo dnf install procps-ngorsudo yum install procps-ng
macOS
free is not available by default on macOS. You can install it using Homebrew:
brew install coreutils
After installation, you might need to use gfree instead of free to avoid conflicts with the built-in free command.
Windows
free is not available on Windows. You can use tools like Task Manager or PowerShell cmdlets (Get-ComputerInfo) for memory information.
Core Concepts
- total: Total amount of physical RAM in the system.
- used: Amount of memory currently being used by processes.
- free: Amount of memory that is completely unused.
- shared: Memory used by
tmpfs(RAM-based file system). - buff/cache: Memory used by the kernel for buffering and caching disk I/O. This memory can be reclaimed if applications need it.
- available: An estimate of how much memory is available for starting new applications, without swapping. This includes
freememory plus a portion ofbuff/cachethat can be easily freed. This is often the most useful metric for understanding available memory.
Commands / Usage
Basic Memory Information
-
freeDisplay memory usage in kibibytes (KiB).freeOutput example:
total used free shared buff/cache available Mem: 8071140 3876580 1385444 211884 2809116 3673940 Swap: 2097148 0 2097148 -
free -bDisplay memory usage in bytes.free -bOutput example:
total used free shared buff/cache available Mem: 8462467072 4064718848 1452642304 222119936 2945105920 3852767232 Swap: 2199023104 0 2199023104 -
free -kDisplay memory usage in kibibytes (KiB). This is the default.free -k -
free -mDisplay memory usage in mebibytes (MiB).free -mOutput example:
total used free shared buff/cache available Mem: 7882 3786 1353 207 2743 3588 Swap: 2047 0 2047 -
free -gDisplay memory usage in gibibytes (GiB).free -gOutput example:
total used free shared buff/cache available Mem: 7 3 1 0 2 3 Swap: 2 0 2 -
free -hDisplay memory usage in human-readable format (e.g., KB, MB, GB).free -hOutput example:
total used free shared buff/cache available Mem: 7.9Gi 3.8Gi 1.3Gi 207Mi 2.7Gi 3.6Gi Swap: 2.0Gi 0B 2.0Gi -
free -lDisplay detailed memory information, including Huge Pages.free -l -
free -s 5Update memory usage every 5 seconds. PressCtrl+Cto exit.free -s 5 -
free -c 10Display memory usage 10 times and then exit.free -c 10 -
free --wideDisplay the output on a wider terminal, showing all columns without wrapping.free --wide -
free --siDisplay units in powers of 1000 (e.g., KB, MB, GB) instead of powers of 1024 (KiB, MiB, GiB).free --si
Displaying Specific Information
-
free | grep MemShow only the memory line.free | grep Mem -
free -m | awk '/^Mem:/ {print $4}'Print only the free memory in MiB.free -m | awk '/^Mem:/ {print $4}' -
free -h | awk '/^Mem:/ {print $7}'Print only the available memory in human-readable format.free -h | awk '/^Mem:/ {print $7}'
Common Patterns
-
Checking available memory in GB, updating every 2 seconds:
free -g -s 2 -
Getting total RAM in MiB and piping to a script:
total_ram_mib=$(free -m | awk '/^Mem:/ {print $2}') echo "Total RAM: ${total_ram_mib} MiB" -
Monitoring memory usage continuously in human-readable format:
watch -n 1 free -h(Requires
watchto be installed:sudo apt install watchorbrew install watch) -
Checking swap usage:
free | grep Swap -
Calculating percentage of used memory:
free -m | awk '/^Mem:/ {printf "%.2f%%\n", $3/$2*100}'This calculates the percentage of used memory from the
Mem:line.
Gotchas
freevsavailable: Thefreecolumn is often misleading.availableis a much better indicator of how much memory can actually be used for new applications, as it accounts for reclaimable cache and buffer memory.- Buff/Cache is not wasted: The
buff/cachememory is actively used by the Linux kernel to speed up disk operations. This memory is not lost; it will be freed automatically if applications require more RAM. - Swap usage: High swap usage can indicate that your system is running out of physical RAM and performance may degrade significantly.
- macOS
gfree: If you installcoreutilson macOS, remember to usegfreeinstead offreeto access the GNU version of the command. - Output interpretation: The units can be confusing. Always pay attention to the unit displayed (bytes, KiB, MiB, GiB, or human-readable) and use the appropriate flags (
-b,-k,-m,-g,-h) to get the desired output.