Sunday, December 20, 2009

The case against Swap

Swap, that extra partition you keep around in case you run out of memory, is actually, in most situations, a bad thing to have. Here's why.

Why swap was a good thing.

1. Hard drives and RAM memory used to have similar speeds. RAM was faster, but not by so much that it meant the system was unusable if it was run off of the hard drive instead. When this is the case, it makes sense to use the hard drive as a backup, but slower memory.

2. Applications tended to fill the RAM. Imagine you have a system with 16MB of memory. You have a word processor that uses 12MB and an image editor that uses 12MB. To have both open simultaneously, you need to have an extra 8MB of storage somewhere. It makes sense to use the hard drive. This way, you can keep the entire word processor in memory when you're using it and when you switch to the image editor, the system can swap the word processor to disk and bring the image editor back to RAM. You'll experience a word processor and image editor that both run at full speed and the only slowdown you'll see is when you switch applications.



Why swap is (now) a bad thing.

1. RAM memory is now so much faster than the hard disk that the system is unusable if it runs off of the hard drive. Maybe this will change again someday as SSD's pickup speed, but for now, it's better to have an application crash or fail to open rather than have an almost frozen and completely unusable system.

2. In most cases, no single application consumes a majority of the RAM. Today, a large application might consume 15%-20% of the available RAM on your system. This makes it unlikely that you'll need to have one application's memory on disk while the other one is in RAM. Running out of RAM today is more likely due to having far too many applications open. The solution is not to mask it by swapping to disk, but rather to either change user behavior (keep fewer applications open) or install more RAM.

3. If the machine starts to thrash (i.e., access the swap continuously), it can be rough on a hard drive, especially if you're using a SSD, where the number of writes is limited.



Why not having swap is a good thing.

It can stop bugs. I had a buggy version of Adobe Acrobat that after you "closed" it, it would remain open in the background and slowly consume memory. After a couple of hours, all of the RAM would be gone and it would start in on the swap. After a couple of more hours, the system would be thrashing the disk and completely unusable. Without swap, after Acrobat used up all of the RAM, it would request more, the request would fail, and it would crash (and close). Of course, it would have been better if Adobe had produced a bug-free version of Acrobat, but consider in this case not having swap prevented my system from being brought down due to a buggy application.



Why not having swap could be a bad thing.

If you have a particular use case that requires you to use more memory than you have RAM, then you will need to have swap. But let me suggest that instead of permanently keeping around a swap partition, use a swap file instead and only activate it when you know you're going to be needing it.


My conclusions.

I think for most users, swap is an outdated concept and causes more trouble than its worth. And your applications probably don't require as much memory as you think. A couple of real-world examples:

I have an old P4 system with 384MB of RAM and no swap that's been running fine for word processing and Internet use for years. It has OpenSUSE 11.1 on it with XFCE.

I also have a laptop with 512MB of RAM and no swap, running OpenSUSE 11.1 with KDE 3.5. I use it for word processing, web surfing, watching Hulu and YouTube videos. I even use a TV tuner card to record shows. I used this machine to write a 100 page technical manual filled with graphics (both vector and bitmapped). Again, I've had no problems due to the lack of swap.

No comments:

Post a Comment