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

Mark Francis mark at canb2c.com
Wed Jun 30 02:52:24 GMT 2004

Martijn van Oosterhout wrote:

>On Wed, Jun 30, 2004 at 09:54:21AM +1000, Martin Pool wrote:
>>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.
>Actually, that's the reason I allocate about 64MB (or even 16MB on some
>machines) of swap, nowhere near the actual amount of RAM I have. Just
>to allow the kernel to get rid of some of those pages that never get
>used again but it can't throw out. It's small enough that reading it
>all in wouldn't take much time. If my machine starts swapping
>constantly I need more memory. Fortunatly that doesn't happen...
>This idea of scaling swap according to the amount of physical RAM you
>have always seemed a little odd to me...

The trade off between RAM and swap space is a cost/performance issue.  
Swap space is cheap so allocating plenty of this gives you plenty of 
virtual memory at little cost. The problem is that if the working set 
size (memory being actively used) of all running applications, daemons 
and the kernel exceeds the available RAM size then thrashing occurs 
which can dramatically affect performance. At this point more RAM is 
needed or memory requirements reduced somehow.  This can be complicated 
by programs that increase their working set size if they detect 
underused RAM.

To manage RAM size you could measure or estimate the maximum working set 
size to determine RAM requirements or alternatively you could monitor 
working set size or paging rates and upgrading RAM when a problem is 
seen.  The key metrics for setting RAM and swap size are maximum ever 
virtual memory requirements (RAM + swap) and maximum working set size 
(RAM).  When memory was expensive these metrics were monitored closely.  
These days extra RAM can be used to provide a buffer.


More information about the linux mailing list