1

Say RAM is 100% used, some by disk-cache and some by applications. There is plenty of swap (on a hard disk available). If Linux decides to evict a page that contains application data, it would be moved to swap space.

But if Linux decides to evict a page in RAM that contains cached disk data, does it move that page to swap, or simply deletes it?

2

TL;DR If page is a cache for disk reading, it never goes to swap.

Your question signifies you already know how swap works (or rather - how virtual memory works). Now the simple critical step in understanding disk cache, is to note it is handled in exactly the same way. There is no "application data" and "disk cache", it's just all a single virtual memory mechanism.

Each page is supported by permanent storage: instead of declaring the page as "disk cache", you can also declare "this page is supported by /var/spool/mail/root". Instead of declaring the page as "application memory" you can also declare "this page is supported by swap space" (aka "it is anonymous" = it isn't supported by any named file).

If the page is dirty, it needs to be saved to its own permanent storage - independent if it's a named file or swap space. If the page is not dirty, it means by definition that exactly the same bytes are already on page's permanent storage. No need to do anything - OS can give this page for re-use at any time it finds suitable.

What I called here the "supported by" attribute people mostly call memory mapping and what I described here is really how mmap works.

  • Thank you, kubanczyk, for the detailed answer. I am rather confused by what it means to be "handled in exactly the same way", or maybe it's been too long since my OS course. I always thought there is one major difference between disk cache and heap for Gimp (say) - if the page containing Gimp's heap is selected for eviction, it will always be copied to swap, regardless of whether it's clean or dirty, because a copy of this page isn't on swap. But this is not true for clean disk cache pages (just re-used, as you mentioned). Or maybe this is simply a minor detail in the grand scheme of things. – Tosh Mar 4 '16 at 9:49
  • How do you know if Gimp's page isn't clean - doesn't exist unmodified on swap? :) It could be clean. There is a separate kernel thread for saving things to swap and a separate thread for freeing some of the clean pages. – kubanczyk Mar 4 '16 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.