4

We have a RHEL 7 machine, with only 2G of available RAM:

free -g
              total        used        free      shared  buff/cache   available
Mem:             31          28           0           0           1           2
Swap:            15           9           5

so we decided to increase the swappiness to the maximum with vm.swappiness = 100 in /etc/sysctl.conf instead of 10, and used sysctl -p to apply the setting.

After some time we checked the status again:

 free -g
              total        used        free      shared  buff/cache   available
Mem:             31          28           0           0           2           2
Swap:            15           9           5

as we can see despite the new swappiness setting, we see from free -g that the available RAM stays at 2G. Why? What is wrong here?

We expected to see 15G of used swap.

We also checked:

cat /proc/sys/vm/swappiness
100

so everything should work according to the new settings BUT free shows the same situation. What is going here?

2 Answers 2

9

The swappiness setting is working as intended. Increasing swappiness doesn’t cause the system to prefer swap to anything else; increasing swappiness affects the balance between the page cache and swap. When the kernel needs to make physical memory available, it can discard generally use one of two strategies: it can discard pages from the page cache (since their content is on disk), or it can move pages to swap; swappiness determines how much it favours one strategy over another. Setting swappiness to 0 (the minimum) means the kernel will avoid swapping until it hits various high water marks, and evict pages from the page cache instead; setting it to 100 (the maximum¹) means the kernel will consider swapping and evicting the page cache equally.

You’ll only see your new setting make a difference when the kernel needs more memory: you’ll see the amount of swap used increase before the amount of memory used in the cache decreases.

You can’t use swappiness to get the kernel to keep more memory available. Physical memory is always best used rather than left free, so the kernel has no incentive to pre-emptively free physical memory (increasing available memory).

See the RHEL 7 performance tuning guide for more information.


¹ In RHEL 7; more recent kernels allow values up to 200, which are useful for RAM-based swap.

2
  • Stephen, you mention "setting swappiness to 100 means the kernel will always prefer swapping over discarding pages from the page cache". On the other hand Thomas Nyman's answer states setting swappiness to 100 should cause the swap and page cache priorities to be equal. Are you able to comment? Thanks
    – iruvar
    Commented Mar 27, 2019 at 11:59
  • @iruvar thanks, my explanation was indeed incorrect. In practice the effect tends to end up being that the kernel prefers swapping, presumably because of the way it takes previous scanning results into account (I haven’t thought about it enough to say for sure), but the base priorities are indeed equal when swappiness is set to 100. Commented Mar 27, 2019 at 14:37
1

Solution is to increase vm.watermark_scale_factor (great article on this topic)

sudo sysctl -w vm.watermark_scale_factor=1000 should have the effect that you're looking for :

When free memory goes below 10% (where 10 := watermark_scale_factor / 100), the kernel will wake kswapd and will thus swap out memory pages to the swap (or discard cache pages, this actually depends on vm.swappiness) until free memory goes up to 20%

Useful reference : https://www.kernel.org/doc/Documentation/sysctl/vm.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .