free
How much memory is in use, and how much is really free
free prints one table for the whole machine: memory on the first row, swap on the second. People
usually read the free column first, but it is the wrong one to read. Linux keeps file contents in spare
memory as cache and hands it back the moment a program needs it, so on a machine that has been up
for a while, free is small, which is fine. available is the figure to read to find out whether
there is room to start something else, because it counts that cache as reclaimable. used is worked out from it, as
total minus available, so cache is never counted as used.
Every figure here describes the machine rather than a process, so no two machines print the same
table. That includes a container, which reports its host's memory rather than any limit it was given.
For which process is using the memory, see ps and top.
Reading the table
The figures on this page are from one machine at a single moment in time. The commands you run on your own machine will differ in every column. What stays consistent is how the columns relate to each other, which is what the checked examples here show.
See how much memory is in use
free -m
-m gives mebibytes, which reads more easily than the default kibibytes. free is memory holding nothing at all, buff/cache is memory holding file contents the kernel can drop, and available is roughly the two together: how much a new program could have without anything being swapped out. A free far below available is the normal state of a busy machine.
Show output
total used free shared buff/cache available
Mem: 3916 2184 827 12 1099 1731
Swap: 1023 621 402
Confirm that cache is not counted as used
free | awk '/^Mem:/ {print ($2 - $7 == $3) ? "used is total minus available" : "used is not total minus available"}'
Why this example is here: The command in “See how much memory is in use” prints figures that belong to the machine it runs on, so its output is different everywhere and cannot be checked the way the rest of the examples on this site are. This command is here to check what the page says about that output instead. It works the claim out and prints the answer, and that answer is re-run on every change. You are unlikely to need it yourself.
Reads one table and tests used against total minus available. That is how this version of free defines the column, so the memory held as cache, which available includes, is never part of used. Older versions of free worked used out differently, which is why advice written for them disagrees.
Show output
used is total minus available
Name the columns
free | head -1
The header on its own. shared is memory used by tmpfs filesystems and shared memory segments, and it is already counted within buff/cache, so it is not a separate share of the total.
Show output
total used free shared buff/cache available
Split buffers from the page cache
free -w -m
-w prints buffers and cache as two columns instead of one. buffers is raw disk blocks the kernel is holding, mostly filesystem metadata, and is usually small. cache is file contents, plus the kernel's own reclaimable caches, which is why it is larger than the Cached line in /proc/meminfo.
Show output
total used free shared buffers cache available
Mem: 3916 2180 831 12 7 1092 1736
Swap: 1023 621 402
See the columns printed by the wide form
free -w | head -1
Seven columns rather than six, which matters to a script that picks a field by its position: available is field 7 in the normal form and field 8 in the wide one.
Show output
total used free shared buffers cache available
Units
The default is kibibytes, 1024 bytes each. Every unit flag scales the same figures, and none of them changes what is actually measured.
Pick a unit that fits the number
free -h
-h chooses a unit per figure and marks it, so a gigabyte total and a megabyte of shared memory both stay short. The i means powers of 1024. Rounded to two or three significant figures, it is for reading rather than for arithmetic.
Show output
total used free shared buff/cache available
Mem: 3.8Gi 2.1Gi 831Mi 12Mi 1.1Gi 1.7Gi
Swap: 1.0Gi 621Mi 402Mi
Show exact byte counts
free -b
The figures before any rounding. Compare them against a program's own report in bytes, or use them in arithmetic that the rounded forms would throw off.
Show output
total used free shared buff/cache available
Mem: 4106604544 2284474368 874090496 13152256 1152966656 1822130176
Swap: 1073737728 652095488 421642240
Confirm the default unit is 1024 bytes
echo $(( $(free -b | awk '/^Mem:/ {print $2}') / $(free -k | awk '/^Mem:/ {print $2}') ))
Why this example is here: The command in “Show exact byte counts” prints figures that belong to the machine it runs on, so its output is different everywhere and cannot be checked the way the rest of the examples on this site are. This command is here to check what the page says about that output instead. It works the claim out and prints the answer, and that answer is re-run on every change. You are unlikely to need it yourself.
Divides the total in bytes by the total in the default unit. The total does not move between the two runs, so the result is the size of the unit.
Show output
1024
Count in powers of 1000
free --kilo
--kilo, --mega and --giga divide by 1000 rather than 1024, which is what a disk manufacturer means by a kilobyte. The figures come out larger than -k gives, for the same memory.
Show output
total used free shared buff/cache available
Mem: 4106604 2284797 873766 13152 1152966 1821806
Swap: 1073737 652095 421642
Confirm --kilo divides by 1000
echo $(( $(free -b | awk '/^Mem:/ {print $2}') / $(free --kilo | awk '/^Mem:/ {print $2}') ))
Why this example is here: The command in “Count in powers of 1000” prints figures that belong to the machine it runs on, so its output is different everywhere and cannot be checked the way the rest of the examples on this site are. This command is here to check what the page says about that output instead. It works the claim out and prints the answer, and that answer is re-run on every change. You are unlikely to need it yourself.
Divides the total in bytes by the total --kilo printed, as the check on the default unit does. --kilo drops the remainder when it divides, and so does the shell, so the result is the size of the unit.
Show output
1000
Pick a unit that fits, in powers of 1000
free -h --si
--si makes -h count in powers of 1000, so the suffixes lose their i: G rather than Gi. The same memory reads as slightly more, which is worth knowing before comparing free against a figure from a tool that counts this way.
Show output
total used free shared buff/cache available
Mem: 4.1G 2.2G 929M 26M 1.2G 1.9G
Swap: 1.1G 664M 408M
Swap, totals and commitments
Swap is disk standing in for memory. Some swap in use is normal, since the kernel moves out pages that have not been touched by anything for a long time. Swap filling quickly while available is low is the sign of a machine that is short of memory.
Add memory and swap together
free -t -m
-t adds a Total row with memory and swap summed in the first three columns. It tells you how much the machine can hold before it runs out of both.
Show output
total used free shared buff/cache available
Mem: 3916 2180 831 12 1099 1735
Swap: 1023 621 402
Total: 4940 2802 1233
Confirm the Total row is memory plus swap
free -t | awk '/^Mem:/ {t=$2; u=$3; f=$4} /^Swap:/ {t+=$2; u+=$3; f+=$4} /^Total:/ {print ($2 == t && $3 == u && $4 == f) ? "Total is Mem plus Swap" : "Total is not Mem plus Swap"}'
Why this example is here: The command in “Add memory and swap together” prints figures that belong to the machine it runs on, so its output is different everywhere and cannot be checked the way the rest of the examples on this site are. This command is here to check what the page says about that output instead. It works the claim out and prints the answer, and that answer is re-run on every change. You are unlikely to need it yourself.
Sums the first three columns of the two rows from one run and tests them against the Total row. The rounded -m figures can be one out from the sum, which is why this reads the unrounded default.
Show output
Total is Mem plus Swap
Confirm that swap used is swap total minus swap free
free | awk '/^Swap:/ {print ($2 - $4 == $3) ? "swap used is total minus free" : "swap used is not total minus free"}'
Why this example is here: The command in “Add memory and swap together” prints figures that belong to the machine it runs on, so its output is different everywhere and cannot be checked the way the rest of the examples on this site are. This command is here to check what the page says about that output instead. It works the claim out and prints the answer, and that answer is re-run on every change. You are unlikely to need it yourself.
Swap holds no cache, so nothing needs to be left out of its used: it is simply total minus free. The memory row does not work this way, because its used leaves out the cache, and reading the memory row as if it worked like the swap row is where most misreadings of free start.
Show output
swap used is total minus free
See how much memory has been promised
free -v -m
-v adds a Comm row, for committed memory. When a program asks for memory, the kernel agrees to it and counts the amount as committed, but it only hands over real memory when the program first writes to it. The row's first figure is the commit limit, the most the kernel would commit if it were strict about it. The second is how much it has committed to running programs so far, and the third is the limit minus that, negative here. By default Linux commits more than it has, because most programs never use everything they ask for, so a negative figure is ordinary unless the kernel has been set to refuse to overcommit.
Show output
total used free shared buff/cache available
Mem: 3916 2180 831 12 1099 1735
Swap: 1023 621 402
Comm: 2982 5224 -2241
Confirm the Comm row's last figure is the limit minus the commitments
free -v | awk '/^Comm:/ {print ($2 - $3 == $4) ? "free is the limit minus what is committed" : "free is not the limit minus what is committed"}'
Why this example is here: The command in “See how much memory has been promised” prints figures that belong to the machine it runs on, so its output is different everywhere and cannot be checked the way the rest of the examples on this site are. This command is here to check what the page says about that output instead. It works the claim out and prints the answer, and that answer is re-run on every change. You are unlikely to need it yourself.
Tests the third figure against the first two from one run. A negative result means more has been committed than the limit allows, which only matters when the kernel is enforcing the limit.
Show output
free is the limit minus what is committed
See that a 64-bit machine has no high memory
free -l | grep "^High:"
-l splits memory into low and high, a distinction left over from 32-bit kernels. A 32-bit kernel can only reach part of physical memory directly, and on a 32-bit PC anything past the first 896 MiB was high memory, which the kernel had to map in temporarily each time it used it. A 64-bit kernel can reach all physical memory directly, so none of it is high: the High row is all zeros on every 64-bit machine, and the Low row above it is simply all of memory.
Show output
High: 0 0 0
Watching and scripting
Print a fresh sample every second
free -m -s 1 -c 3
-s repeats every so many seconds and -c stops after that many samples. Without -c it runs until interrupted, which is a cheap way to watch memory while a job runs. Fractions of a second work too.
Show output
total used free shared buff/cache available
Mem: 3916 2180 831 12 1099 1735
Swap: 1023 621 402
total used free shared buff/cache available
Mem: 3916 2178 833 12 1099 1737
Swap: 1023 621 402
total used free shared buff/cache available
Mem: 3916 2178 833 12 1099 1737
Swap: 1023 621 402
Confirm -c sets the number of samples
free -m -s 1 -c 3 | grep -c "^Mem:"
Why this example is here: The command in “Print a fresh sample every second” prints figures that belong to the machine it runs on, so its output is different everywhere and cannot be checked the way the rest of the examples on this site are. This command is here to check what the page says about that output instead. It works the claim out and prints the answer, and that answer is re-run on every change. You are unlikely to need it yourself.
Counts the memory rows the same command prints. One per sample, and it exits on its own after the third.
Show output
3
Put the figures on one line for a log
free -L
-L prints swap in use, cache, memory in use and free memory on one line, each after a label, which suits a log file written from cron where one sample per line keeps the file easy to grep.
Show output
SwapUse 636812 CachUse 1125944 MemUse 2230756 MemFree 853780
Get one figure for a script
free -m | awk '/^Mem:/ {print $7}'
Only the available figure, in mebibytes, which is the one a script should test before starting something large. Matching the row by its label rather than its line number keeps it working if the output gains a row.
Show output
1737
Where the figures come from
Read the kernel's own figures
grep -E '^(MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapTotal|SwapFree):' /proc/meminfo
free formats /proc/meminfo, a file of around fifty lines of which it uses a handful. MemAvailable is the kernel's own estimate, and free prints it unchanged as available.
Show output
MemTotal: 4010356 kB
MemFree: 853036 kB
MemAvailable: 1778932 kB
Buffers: 7564 kB
Cached: 960576 kB
SwapTotal: 1048572 kB
SwapFree: 411760 kB
Confirm free's total is the kernel's MemTotal
[ "$(free -k | awk '/^Mem:/ {print $2}')" = "$(awk '/^MemTotal:/ {print $2}' /proc/meminfo)" ] && echo "free reads MemTotal from /proc/meminfo"
Why this example is here: The command in “Read the kernel's own figures” prints figures that belong to the machine it runs on, so its output is different everywhere and cannot be checked the way the rest of the examples on this site are. This command is here to check what the page says about that output instead. It works the claim out and prints the answer, and that answer is re-run on every change. You are unlikely to need it yourself.
Compares the two totals as numbers. Both are in kibibytes, and the total does not move while the machine runs, so they match exactly.
Show output
free reads MemTotal from /proc/meminfo
See whether this container has a memory limit
cat /sys/fs/cgroup/memory.max
Inside a container, free reads the host's /proc/meminfo and so reports the host's memory, whatever limit the container was started with. The limit, if there is one, is here, in bytes. max means none was set. On a Debian machine that is not a container, the file does not exist at the top of the hierarchy.
Show output
max