[clug] to swap or not to swap that is the question

Martin Pool mbp at sourcefrog.net
Tue Jun 29 23:54:21 GMT 2004

On 29 Jun 2004, Richard <richard_c at tpg.com.au> wrote:
> Kim Holburn wrote:
> >Suppose you have a system with say 1G of RAM and swap of 2G.  How is 
> >that better than say a system with 2G of RAM (or even 3G RAM).
> >
> >Is it because HD is so much slower that the wait causes everything to 
> >slow down?
> I'll throw in a comment here. I think the bottom line is that ideally 
> the set of memory pages that are used should always be kept in RAM, and 
> if you /really/ need them, then putting them in swap is better then not 
> having them at all. Therefore, you should always have as much RAM as 
> practical, and enough swap space to cope with bizarre spikes (HD space 
> being cheap and all). You should never have swap space in preference to 
> more RAM though.
> I've heard of admins who are so insistent that everything should be in 
> RAM at all times that they don't run any swap at all and use crashing 
> processes as an indication to add more RAM. The theory runs that swap is 
> only a short step from thrashing the disk, which will cause performance 
> to nose dive, which is only a short step from crashing the process 
> anyway. I think this a bit extreme.

Scary.  Why not see whether anything is swapped and use that as an
indication to add more RAM?

If you want programs to stay in memory you should use mlock(), not

The misunderstanding lots of people have is that swap is somehow worse
or slower than regular disk IO.  It's not.  It's just the same, or if
anything slightly faster.

When the kernel needs a free page and doesn't have one already, it
needs to page something out.  This is the steady state for the machine
-- free mem normally hovers at a couple of percent of total, by

There are a few options.  One is to discard a clean page (e.g. a
program text, or file that's already been flushed).  One is to write a
file-backed page to disk.  Another is to write a non-file-backed page
to swap (e.g. part of some process's heap).  The kernel tries to chose
the right one.

If you don't have swap, you remove one of the options.  It can't do
anything with non-file-backed memory; it has to stay in RAM.  So the
kernel has to discard a clean page, or write out a dirty page instead.

Suppose you start a program at bootup that allocates and writes to 4MB
of memory, and then never touches it again.  In fact, suppose the
process sleeps and never runs again at all.  With no swap space, that
memory has to stay in RAM, using up space that could be used for disk
cache, holding programs, or other data.  With swap, the kernel can
write it to swap in the background, and never need to read it back in.
So the cost is very small and it frees up memory.

Obviously you don't have a program which does exactly that, but you
probably do have some few pages like that.  So allocate a bit of swap
and let the kernel do its thing.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.samba.org/archive/linux/attachments/20040630/d227db27/attachment.bin

More information about the linux mailing list