views:

218

answers:

1

My server has 8Gigs of RAM and 8Gigs configured for swap file. I have memory intensive apps running. These apps have peak loads during which we find swap usage increase. Approximately 1 GIG of swap is used.

I have another server with 4Gigs of RAM and 8 Gigs of swap and similar memory intensive apps running on it. But here swap usage is very negligible. Around 100 MB.

I was wondering what are the exact conditions or a rough formula based on which Linux will do a swapout of a process memory in RAM to the swap file. I know its based on swapiness factor. What else is it based on? Swap file size? Any pointers to Linux kernel documentation/source code explaining this will be great.

+2  A: 

Linux (or any other OS) divides memory up into pages (typically 4Kb). Each of these pages represent a chunk of memory. Usage information for these pages is maintained, which basically contains info about whether the page is free or is in use (part of some process), whether it has been accessed recently, what kind of data it contains (process data, executable code etc.), owner of the page, etc. These pages can also be broadly divided into two categories - filesystem pages or the page cache (in which all data read/written to your filesystem resides) and pages belonging to processes.

When the system is running low on memory, the kernel starts swapping out pages based on their usage. Using a list of pages sorted w.r.t recency of access is common for determining which pages can be swapped out (linux kernel has such a list too).

During swapping, Linux kernel needs to decide what to trade-off when nuking pages in memory and sending them to swap. If it swaps filesystem pages too aggressively, more reads are required from the filesystem to read those pages back when they are needed. However, if it swaps out process pages more aggressively it can hurt interactivity, because when the user tries to use the swapped out processes, they will have to be read back from the disk. See a nice discussion here on this.

By setting swappiness = 0, you are telling the linux kernel not to swap out pages belonging to processes. When setting swappiness = 100 instead, you tell the kernel to swap out pages belonging to processes more aggressively. To tune your system, try changing the swappiness parameter in steps of 10, monitoring performance and pages being swapped in/out at each setting using the "vmstat" command. Keep the setting that gives you the best results. Remember to do this testing during peak usage hours. :)

For database applications, swappiness = 0 is generally recommended. (Even then, test different settings on your systems to arrive to a good value).

References:
http://www.linuxvox.com/2009/10/what-is-the-linux-kernel-parameter-vm-swappiness/
http://www.pythian.com/news/1913/

Sudhanshu
The terminology may have changed but in the olden days "swapping"meant swapping out whole processes while "paging" meant swapping outindividual memory pages. Most OSs do paging, the only OS I can recall that does "real" swapping is UNICOS.
Per Ekman
I still encounter some old-timers who maintain the old definition of swapping, but it is very rare now. Full process swapping is such an uncommon technique in current systems that "swap" started to suffer definition rot and became a useless term, which was redefined to be useful again such that it basically means the same as "paging." I occasionally run into the old sense of swap called "full process paging," which is sort of a funny turn of the wheel of redefinition but tends to be understood.
wrosecrans
With shared libraries, copy-on-write fork, etc., exactly what is meant by swapping out an entire process becomes a little fuzzy.
Ben Voigt