Reading memory usage with free
- Version
- unversioned
- Review
- Draft
Summary#
free summarizes physical memory and swap from /proc/meminfo. The available column estimates how much memory new programs can use without swapping.
Syntax#
free -h
free -m
free -s 5
free -h --total
-h picks readable units, -m prints mebibytes, -s repeats at an interval and --total adds a line that combines memory and swap.
Example#
(
set -eu
LC_ALL=C free -b | awk '
NR == 1 { print $1, $2, $3, $4, $5, $6 }
$1 == "Mem:" { print ($2 > 0 && $7 > 0 && $7 <= $2) ? "available memory is within the total" : "unexpected values" }
'
)
Expected output#
The numbers differ on every system, so the script checks them instead of printing them.
total used free shared buff/cache available
available memory is within the total
Common mistakes#
A small free column does not mean the system is out of memory. Linux uses spare memory for file caches, which it can reclaim, so judge pressure by available. Inside a container, free normally reports the host or virtual machine rather than the container's memory limit; read the cgroup files or the container runtime's statistics for the limit.
Version applicability#
This entry describes free from procps-ng 4. The available column appeared in procps-ng 3.3.10. macOS has no free command.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0