Linux – What does the vm.swappiness parameter really control

kernellinuxmemoryswap

Per the kernel documentation:

This control is used to define how aggressive the kernel will swap
memory pages.  Higher values will increase aggressiveness, lower values
decrease the amount of swap.

However this is kind of vague. I'm trying to figure out exactly what the parameter ultimately controls. I know it adjusts how aggressively the kernel tries to swap out pages, but what decision making process in the kernel code does it affect?

Does it adjust how long ago a page must have been accessed before the kernel swaps it out? If so what does the default value of 60 represent? And how much is changed by a increment/decrement of 1 (a formula would be nice)?
Or does it swap out pages based on their access frequency?
Or something else?

Best Answer

Since kernel version 2.6.28, Linux uses a Split Least Recently Used (LRU) page replacement strategy. Pages with a filesystem source, such as program text or shared libraries belong to the file cache. Pages without filesystem backing are called anonymous pages, and consist of runtime data such as the stack space reserved for applications etc. Typically pages belonging to the file cache are cheaper to evict from memory (as these can simple be read back from disk when needed). Since anonymous pages have no filesystem backing, they must remain in memory as long as they are needed by a program unless there is swap space to store them to.

The vm.swappiness option comes into play in get_scan_count() defined in mm/vmscan.c. get_scan_count() determines how aggressively the anonymous and file LRU lists should be scanned when looking for pages to evict. The value of each case is is determined by the floating average of recently rotated and recently scanned ratios where more recent references have more weight than older ones in order take into account the changing workload of the system.

The vm.swappiness is a modifier that changes the balance between swapping out file cache pages in favour of anonymous pages. vm.swappiness is the priority value given to anonymous pages, by default set to 60. The file cache is given an priority value of 200 from which the vm.swappiness modifier is deducted (file_prio=200-anon_prio). This means that, by default, the priority weights stand moderately in favour of anonymous pages (anon_prio=60, file_prio=200-60=140). However, when the system is close to an out-of-memory condition, the both anonymous and file LRU lists are scanned equally, unless vm.swappiness is set to zero.

When vm.swappiness is set to 100, the priorities would be equal (anon_prio=100, file_prio=200-100=100). Setting vm.swappiness to zero will prevent the kernel from evicting anonymous pages in favour of pages from the file cache.