A machine's memory is shared by programs, the kernel and the file cache.
free -h shows how much goes where. On the 512 MiB training machine:
|
total |
used |
free |
shared |
buff/cache |
available |
| Mem |
458Mi |
153Mi |
5.4Mi |
964Ki |
320Mi |
304Mi |
| Swap |
0B |
0B |
0B |
|
|
|
Columns of free
| Column |
Meaning |
total |
memory the kernel manages |
used |
taken by programs and the kernel |
free |
not used by anything |
shared |
shared process memory and tmpfs |
buff/cache |
page cache and buffers — copies of disk data in memory |
available |
how much can go to new programs without swap |
total is below 512 MiB: the kernel took part of the memory at boot for
its code and internal tables (sudo dmesg | grep Memory:).
Page cache and available
The kernel keeps copies of files it has read and written in memory — the
page cache. Reading again comes from memory, without the
disk. Almost all free memory goes to the cache, so free on a running
machine is close to zero. When a program needs memory, the kernel evicts
cache pages: their copy is on disk anyway.
available is free memory plus the part of the cache the kernel can
release. That is the real reserve. An experiment on the training machine:
read many files, then drop the cache.
| Moment |
free |
buff/cache |
available |
after cat /usr/bin/* > /dev/null |
5.4Mi |
320Mi |
304Mi |
| after dropping the cache |
266Mi |
59Mi |
304Mi |
free grew fifty times, while available did not change: the cache was
available to programs before the drop too. The same numbers in KiB are in
/proc/meminfo:
student@ubuntu:~$ grep -E '^(Mem|Cached)' /proc/meminfo
MemTotal: 469236 kB
MemFree: 5540 kB
MemAvailable: 311944 kB
Cached: 301900 kB
RSS is how much of a process's memory is in RAM right now, in
KiB. VSZ is how many addresses it reserved; most of them may be unused.
Process memory is judged by RSS: the RSS column in ps, RES in
top, VmRSS in /proc/<pid>/status.
student@ubuntu:~$ ps -o pid,rss,vsz,pmem,comm -C systemd-journald,sshd
PID RSS VSZ %MEM COMMAND
275 16792 42256 3.5 systemd-journal
773 8244 12028 1.7 sshd
The sum of all RSS can exceed used: shared libraries sit in memory once,
but count in full in the RSS of each process.
Swap
Swap is disk space where the kernel moves rarely needed
program pages to free memory. The disk is many times slower than memory:
while swap helps, programs keep working, only slower. Without swap the
kernel calls the oom-killer as soon as memory runs out.
The training machine has no swap: swapon --show prints nothing, and
free shows Swap: 0B 0B 0B. Swap can be a disk partition or a file; a
file is easier to add and remove.
Swap activity shows in vmstat 1: si is how many KiB per second are read
back from swap into memory, so is how many are written to swap. A 380 MiB
process under memory shortage produced these lines (some columns omitted):
| r |
b |
swpd |
free |
si |
so |
wa |
| 0 |
1 |
131688 |
5920 |
152012 |
150756 |
56 |
| 0 |
1 |
130904 |
5096 |
135084 |
130312 |
53 |
si and so at about 150 MiB per second, wa above 50: the machine waits
for the disk more than it works. Who holds how much in swap is VmSwap in
/proc/<pid>/status.
vm.swappiness
The kernel parameter vm.swappiness (0 to 200, 60 on Ubuntu) sets how
eagerly the kernel, short of memory, moves program pages to swap compared
with cache pages. A lower value keeps programs in memory whole for longer.
Servers often use 10.
sysctl vm.swappiness
sudo sysctl vm.swappiness=10
echo 'vm.swappiness = 10' \
| sudo tee /etc/sysctl.d/90-swappiness.conf
sudo sysctl --system
A value from a file in /etc/sysctl.d/ survives a reboot. Kernel
parameters in detail — sysctl.