>> It sounds like adding physical RAM is better than enabling swap in
>> every way.  I'll stay in the anti-swap camp.
>
> I don't see why it has to be one way *or* the other...
>
> Yes more RAM is always going to be better than more swap, RAM is just
> way faster than disk, however byte-per-byte, disk is cheaper.
>
> The whole reason why we have swap.. back in the olden days, some
> programs needed more RAM than perhaps the system provided.  Some of
> these program were written with this in mind, and actually handled this
> by manually writing some of it's data to disk, then freeing that data
> from RAM, doing something, then when it needed the disk data, reading it
> back into RAM (after having freed the previous data).  This is of course
> cumbersome.  Enter virtual memory operating systems, which basically
> treat fast memory (RAM) and slow memory (disk) as one flat memory pool.
> Then the program it all looks like memory, and the OS does the paging in
> and out to disk.
>
> By now you would think "oh, but if I just had one system that had a more
> RAM than i would ever use simultaneously, then I don't need swap, right?
> Well, not exactly, because modern operationg systems also do something
> called filesystem caching.  What this does is, recently, and often used
> (parts of) files on the filesystem are kept into fast RAM, so when a
> program needs that data it can be fetched from cached RAM instead of
> hitting slower disk.  Ok, that's nice, but what does that have to do
> with swap?  Well, not only does Linux keep track of what files are used 
> often, it
> also keeps track of what pages of virtual memory are *not* used often.
> Say you started some program a long time ago, or some program launches
> at boot time, but that program sleeps and doesn't do anything for a long
> time.  Now ordinarily that program would just sit there taking up RAM.
> Now you are running some other programs, and these programs are very
> actively hitting the disk.  Now Linux would love to use more RAM for
> caching those disk hits, but that program you haven't touched in hours
> is taking up RAM doing nothing.
>
> That's where swap comes in.  Linux would like to take that sleeping
> process and swap some of its pages out to disk, so it can use the freed
> RAM for more cache, and therefore speed up the programs that are
> actually being used.  Then, if the barely used process ever does wake
> up, Linux can expire some cache and put it back into RAM.
>
> In this case swap *is* good because it's making more efficient use of
> RAM by swapping out seldom-used processes and using that RAM to cache
> often-accessed files.
>
> So a healthy combination of swap and RAM *can* be a good thing.  If,
> however, you have so much RAM that you can run every program you'd ever
> run simultaneously with every file you'd ever access cached in RAM then
> I wouldn't worry about swap ;)

So swap isn't treated exactly like RAM.  It actually has special
handling in Linux which makes it beneficial to have on almost any
Linux system?  According to Alan, things get very bad when a Linux
system hits swap.  How can behavior like this be beneficial:

"When a linux machine hits swap, it does so very aggressively, there
is nothing nice about it at all. The entire machine slows to a
painstaking crawl for easily a minute at a time while the kernel
writes pages out to disk, and disk is thousands of times slower than
RAM.

It gets so bad that you can't even run a shell properly to try and see
what's going on and kill the actual memory hog."

Also, aren't you likely to wear out your hard disk sooner using swap?

- Grant

Reply via email to