Linux – How to tweak the kernel for total swap out

kernellinuxsettingsswap

I would like to deploy the following swapping policy:

  • By default all pages in memory should also be in swap space.
  • When a page in memory is changed (i.e. dirty), the page should be written out as soon as possible, but with lower priority than other processes.
  • if a certain configurable watermark is reached, (let's say 80% of pages are dirty), the priority will be equal as other processes.

Is this kind of swapping policy possible with the linux kernel? If so, how do I set the kernel settings to achieve this?

Edit:

Obviously the reason for this is to reduce the number of pages that need to be swapped out. Only dirty pages need to be written to disk, and this happens in the background over time. Therefore when page misses occur (i.e. the page is not in memory), there is no need to write any pages from memory to disk, but only from disk to memory. Therefore it reduced the probability of i/o bottlenecks because both swapping in and swapping out try to access the disk simultaneously.

Best Answer

You can set the value of /proc/sys/vm/swappiness to control the ratio of segments of data swapped to the segments of data kept in memory. A value of 0 completely avoids swapping at all costs.

This can be done using either:

  • echo 0 > /proc/sys/vm/swappiness
  • sysctl -w vm.swappiness=0
  • Storing that setting in /etc/sysctl.conf

Generally, using just a little swap is not a bad thing. Free memory can be used for caching data read from disk, and the system can plan ahead for a sudden need of lots of memory by an application.

When too many programs are swapped however, there is a lot of disk related activity during every program switch which really makes everything slow down. Before something can be used, it needs to be loaded back into memory.

Disks reads are horribly slow compared to memory access, as it takes significantly longer for the data to arrive. The system has to schedule the read between the other read/write requests, the drive starts making attempts to find the right cylinder, and finally starts slowly delivering data.

Hence, I think your logic is flawed. Generally, you want to keep programs running in memory, while still keeping enough room for sudden growth. Do not use the swap too often to "write things to disk", because it is neither a backup nor a performance improvement.

Older computers contained less memory and suffered from swapping problems as a result. When many programs were open at once, the system would slow down and you could hear the disk reading and writing in order to the swap file.

Related Question