Ask Slashdot: Linux and Swap Optimization?
I found a nameless submission in the
submission box that asks a fairly good
question:
"I'm installing Linux for the first time on Intel
hardware (I've installed Linux for PPC and BSD for Intel
and 68K) but I am confused on how the swap partition
should be handled for optimum performance. The Linux
Installation Guide says I should have my swap at least as
big as my RAM and that the swap partition should not be
bigger than 127MB. Also a "rule of thumb" answer I got
from my BSD using friends is that my swap should be twice
the size of my RAM. Since I have 128MB of RAM should I
have two swap partitions? Also how will swap drive
performance affect system performance? Would a Fast SCSI
(20MB/s) swap drive be any different than a Wide SCSI
(40MB/s) swap drive? There seems to be little information
on how to 'tune' Linux swap for machines with 100+ MB RAM."
Unlike most, I'm one who believes in investing as much disk to swap as you can afford. 2xRAM is my minimal swap space.
/usr/src/linux/Documentation/Changes in 2.2.10 only states that the 128MB limit is gone, but doesn't list the new physical limit -- I've used up to 1024MB in one swap partition so I know that it can get pretty big. I wouldn't be surprised to find that the limit is 2048MB to conform with the 32 bit file size limit in ext2fs simply for parity with swap files in mkswap.
With Linux kernels previous to (I believe) 2.0.36 you must make each swap partition no larger than 128M, though you may have an arbitrary number of swap partitions or files (and yes, Linux can use swap files in the filesystem instead of individual partitions, but at a performance penalty).
Older 2.0.x, 1.2.x, and 1.0.x kernels must use individual 128MB swap partitions or files (actually, I seem to recall that 1.0.x kernels didn't support swap files, only partitions), but don't know if the larger swap partition support is limited to 2.2.x kernels or the later 2.0.36/37 kernels.
Now for why I like lots of swap: You don't have to use it, but when you need it you REALLY need it! Your VM space is only as large as core and swap. Yes, I know the kernel can address anywhere within the 4GB boundary, but the total number of usable pages is limited to your physical RAM plus swap space! This is important to consider if you ever want to run lots of processes which sleep until asynchronous I/O events wake them up. If you want to start lots of processes and let them go off to sleep, having plenty of VM space makes sense. The machine will only thrash if there's memory contention among processes in a run state, so it's safe (and desirable) to let sleeping processes page out to swap while the machine uses physical core for running code.
Also: consider the possibility of a small application using huge datasets. I run into this at work all the time. We have clustered machines each with 1/2GB of RAM and 1GB of swap. As long as each process only keeps dirty pages of the dataset in core, one can have multiple program instances crunching through different datasets even though together they're exceeding total physical core. Since these programs are stepping linearly through the data, instead of randomly seeking different pages like in a program doing long jumps, this can actually work well. Memory contention occurs not when you exceed total physical ram, but when running processes request pages that have been flushed to disk!
If you're careful and selective about how you use the extra swap space you can wildly exceed physical core without significant performance loss. Food for thought.
To get better performances for Linux machines, subscribe to the Linux-perf mailinglist.
To subscribe, send email to majordomo@www.klinik.uni-mainz.de and put "subscribe linux-perf" (without the quotes) in the message body.
Muchas Gracias, Señor Edward Snowden !
You asked three seperate questions, so I think I'll answer them in order:
First off, the 127MB limit was for the older 2.0 series of kernels. The new 2.2 series (and dists based on them) has a much higher limit (I can't remember exactly what is it, but it's ALOT bigger (maybe like 1GB or more)). So, you can make a single swap partition of 256MB or so. If you are using a 2.0-based dist, you should make multiple partitions, each no bigger than 127MB.
IIRC, linux is like the newer Solaris 2, IRIX, and HPUX systems in that swap space is added to physical RAM to come up with the Total Virtual Memory allotable on the system. Under older BSD4.2-based systems (I don't know about current Free/Net/OpenBSD stuff), swap space equal to physical RAM was used to "shadow" the physical memory (and improve swap performance), and thus your Total Virtual Memory was equal to the amount of swap space you had.
For Linux 2.2, the rule of 2x physical memory is perhaps a bit overkill. I prefer the Solaris guidelines: you should have at least 128MB of total swap+physical for a typical end-user system, with 256MB for a developer/power user, and 512MB for someone using high-end graphics (the X-server gets big) or doing serious work. The point here is: swap space is slow. You should have enough swap space to hold applications that you aren't currently using, but huge amounts aren't useful (unless you have an application that likes to map large amounts of Virtual Memory, but doesn't actually use it). For most of us, I think no more than 256MB of swap is best.
Swap being slow, putting your swap drive on the fastest possible disk is a good way to enhance performance. In your case, the performance characteristics of the drive are more important than the interface. Thus, a new drive (6.5ms / 10,000RPM) with a FastSCSI interface is preferable to using an older drive (perhaps 9ms/7200RPM) with WideSCSI. Striping swap is also a good idea, though you should use drives of similar speed.
Also, note that linux can swap to a file, as well as to a raw partition. However, swapping to a partition is faster, and preferable.
-Erik
There are always four sides to every story: your side, their side, the truth, and what really happened.