vm.swappiness sets how much the kernel prefers to swap out application memory versus dropping file cache when it has to free RAM. It is not a "start swapping at X% RAM used" threshold, which is a common misunderstanding. When RAM isn't under pressure, nothing gets swapped at any value.
How it works. Under memory pressure, the kernel can free RAM in two ways:
- Drop file cache. These are cached pieces of files on disk. It's cheap, but if the files are needed again they must be re-read from disk.
- Swap out anonymous memory. This is process heap and stack, which has no file behind it, so it has to be written to swap first.
Swappiness weights the choice between the two. On the 3.10 kernel that OpenVZ 7 uses, the range is 0 to 100, and the ratio is roughly swappiness : (200 − swappiness) for anonymous memory versus file cache:
| Value | Anon : file weight | Behaviour |
|---|---|---|
| 0 | 0 : 200 | Swap only when there is almost no free memory or cache left to drop. High risk of OOM kills on a busy host. |
| 1 | 1 : 199 | Swap as a last resort, but it still works as a safety net. |
| 10 | 10 : 190 | Light swapping. Cache is dropped first, and cold idle memory can still be swapped. |
| 60 | 60 : 140 | The default. Fairly willing to swap idle process memory to keep cache. |
| 100 | 100 : 100 | Swap and cache are treated equally. Aggressive swapping. |
For your NAT VPS nodes, 10 is a good balance. Many small containers sit idle, so swapping their cold memory is fine, but active VPS memory stays in RAM. Use 1 if you want swap only as an emergency buffer. Avoid 0 on a dense container host, because when RAM runs out the kernel goes straight to the OOM killer and kills container processes instead of swapping a little.
Check and set it:
cat /proc/sys/vm/swappiness # current value
sysctl vm.swappiness=10 # apply now
echo 'vm.swappiness = 10' > /etc/sysctl.d/90-swappiness.conf # persist
Containers have their own value. Each container runs in its own memory cgroup, and each cgroup has its own memory.swappiness. Changing the sysctl may not change containers that are already running, depending on how VZ7 creates them. Check one with:
cat /sys/fs/cgroup/memory/machine.slice/<CTID>/memory.swappiness
If it still shows 60 after you change the host value, a restart of that CT, or a node reboot, will normally make it inherit the new value.
Lowering swappiness doesn't create more RAM; it only changes what gets evicted first. If a node keeps showing real swap activity in the script, that node is oversold on RAM, and no swappiness value will fix that.