Only dirty pages are swapped as we need to retain the pages that have been modified. Also, suppose if a page was modified and was swapped, now if the same page was brought back into physical memory and now there is a need to swap it again but the page has not been modified any further then there is no need to swap this page. Just discard it as this version of the page is already there in the swap file. This saves a good amount of time which otherwise would have been wasted.
Now to implement the above concept, Linux makes use of swap cache.
- A swap cache is nothing but a list of page table entries with one entry per physical page.
- Each entry corresponds to a swapped out page along with the information about the swap file in which the page is being held along with its exact location in the swap file.
- If any page table entry in swap cache is non-zero then it represents a page that is being held in a swap file and that page has not been modified any further.
- If a page has its entry in the swap cache and is further modified then its entry from swap cache is removed.
- This way the cache contains information only on those pages that are not modified since they were last swapped.
To check swap usage, you always can use: vmstat -s