devutils/ATFQ/book ATFQ git

Part II · Your system

17.1 free vs available

alsofreeavailableram usagememinfomemory fullbuff/cache

TL;DR

  • free: memory nobody is using at all, not even as cache.
  • available: what your apps can still get without swapping.
  • Look at available, not free: the available column of the Mem: line in free -h.
free -h

On your machine

alice@laptopexample
$ atfq 17.1
  RAM       10.5 GiB available of 15.5 GiB (68%) · 1.1 GiB completely free
  Cache     7.5 GiB in buff/cache, 604.0 MiB of it shared (tmpfs)
  Swap      256.0 MiB used of 8.0 GiB · swappiness 60

An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.

Pitfalls

Deep dive 3 questions

Why is unused RAM wasted RAM?

RAM is much faster than any disk. When a program reads a file, Linux keeps a copy in the page cache, so the next read comes straight from memory. Empty RAM does nothing for you, so the kernel fills it with cache and takes it back when programs ask for memory.

Taking cache back is cheap when the data is unchanged: it's still on disk, the kernel can drop it. Changed ("dirty") data must be written to disk first, which is slower.

How is "available" calculated?

Since kernel 3.14, the kernel computes it for you: the MemAvailable line in /proc/meminfo, which free shows as "available". Roughly: free memory, plus the page cache and kernel caches that could be reclaimed, minus a safety margin the kernel keeps for itself. It's an estimate, not a promise.

Before that, tools added free + buffers + cache. That overestimated, because part of the cache can't be dropped (tmpfs files, for example).

What is swappiness and when should I change it?

/proc/sys/vm/swappiness (default 60) tells the kernel how to balance two ways of freeing memory: dropping page cache, or moving app memory to swap. Higher values make it more willing to swap. The range is 0 to 100, extended to 200 in kernel 5.8 for fast swap like zram.

Changing it rarely fixes a machine that's short of RAM: it only changes what gets pushed out first. To try a value until the next reboot:

sudo sysctl vm.swappiness=10

To keep it, put vm.swappiness = 10 in a file under /etc/sysctl.d/.

Commands
free vmstat
Paths
/proc/meminfo
See also
17. Memory
17.3 Swap and swappiness
17.2 The OOM killer