I'm quite confused about this behaviour of linux memory caching.              total       used       free     shared    buffers     cachedMem:         15953      14188       1765         64         37      11504-/+ buffers/cache:       2645      13308Swap:         2047       1332        715Shouldn't have non-cache memory priority against caches? In other words: Why is the machine swapping to disk, instead of dropping caches.Can I change this behavior? If yes, how?
Linux doesn't drop FS Caches. Instead Memory starts Swapping
linux;linux kernel;memory;swap;cache
The linux swapping algorithm works with the concept of last recently used pages. Each page in virtual memory has an age associated with it. If the page is being frequently accessed then that page is supposed to be quite young in age while if a page is not being accessed, then that page becomes older. The older the pages get, the more likely they may get swapped out.So if the kernel swaps stuff out, then it's because the age of those pages are (compared to the others) old. If there is enough physical memory for all pages, regardless of their age, nothing will be swapped.The kernel is configured to handle his ressources, such as memory and swap, in the most efficient way, that is possible.I don't think you should change that behavior. But, if you want, you can change the system swappiness. A swappiness setting of 0 means that the disk will be avoided unless absolutely necessary (you run out of memory).From the Kernel Documentation about the value of swappiness:This control is used to define how aggressive the kernel will swap  memory pages.  Higher values will increase agressiveness, lower values  decrease the amount of swap.  A value of 0 instructs the kernel not to  initiate swap until the amount of free and file-backed pages is less  than the high water mark in a zone.In the linux kernel source code the file vmscan.c handles the swappiness value. Here is the interessting part:2018         /*2019          * With swappiness at 100, anonymous and file have the same priority.2020          * This scanning priority is essentially the inverse of IO cost.2021          */2022         anon_prio = swappiness;2023         file_prio = 200 - anon_prio;Anonymous pages are memory mappings with no file or device backing it. This is how programs allocate memory from the operating system for use by things like the stack and heap.File pages mirror the contents of an existing file.As you see in the source code snippet above, the priority to swap file pages is (with a default value of 60) higher than to swap anonymous pages. But, if set to 100, both values have the same priority. If set to 0 the priority difference is as big as possible.You can set the swappiness as follows:echo n >/proc/sys/vm/swappiness... where n is the value from 0-100.