Kernel swap daemon is a background process that begins after a kernel is initialized. The process swaps out pages that are no longer needed to maintain free frames for other pages to be allocated to.
There are 2 types of pages to consider when swapping:
- Clean which can be discarded as they are already stored in the backing store e.g., executable code, pages in swap cache, etc
- Dirty pages which must have data transferred to backing store before being swapped due to being either recently updated or never stored to begin with.
Observation:
- It is inefficient to swap out pages that are needed soon as they will need to be swapped back in soon after being swapped out
Tuning the system
Linux has a swappiness parameter with 0 to 100 scale
- The higher the value the greater the swapping frequency
- Higher swappiness can improve performance by preventing thrashing
- Thrashing is the result of physical memory becoming over committed requiring more swapping, which slows down system due major page faults occurring requiring system resources to resolve.
Policies
Random
Randomly selects page to swap out
Not Recently Used (NRU):
A page that has not been recently used will have an access metadata bit unset and be selected by the algorithm for the replacement.
- Does not guarantee termination as all access bits may be set
- It is assumed the OS will unset bits.

Least Recently Used (LRU)
Works by either:
- Using modified access bit to be a longer last access timestamp for each page
- Orders pages in a doubly linked list in order of access time
Linux uses a variant which acts similarly to NRU but instead of choosing randomly selects the page at end of a queue of global active pages

Clock
- Pages are organized in circular list with “hand” to point to next replacement candidate.
- Algorithm checks if access bit is set, in the case it is then it will be unset and it will check the next candidate in the list otherwise the page is swapped out.