Why swap on Linux is always good, even with tons of RAM
Lately, I’ve been wondering if it’s at all convenient to disable swap on Linux, when memory is so cheap nowadays. Of course, having tons of RAM is a great thing: the more code and data the operating system can maintain into RAM, the faster the system will go. Ideally, you’d want to have as much physical RAM as needed to keep all your frequently-used programs and their data in memory.
I ended up keeping swap active. And here’s why.
But… I do have enough RAM for my programs… so I disabled swap. What’s the dillio?
But, if you had enough RAM to keep the entire working set in memory (in other words, to keep both data and code for all your running programs in main memory, at all times), why would you want swap? More concretely, if you had 512 MB of RAM, and your open programs occupied 490 MB of RAM, why would you need swap?
We all know using the disk as swap is slow. So, many people eliminate swap from their systems as soon as they install lots of RAM. And one of the biggest benefits of having lots of RAM is caching. Linux will attempt to cache disk files as aggressively as possible. Taking a page from the example I just concocted, if all I had left is (512-490) = 22 MB of free RAM, Linux would only be able to cache 22 MB of data.
Caching and program startup speed
Why is that figure important? Because, by now, you’ve probably noticed that the slowest operation on Linux is loading an application (especially big ones, like OpenOffice.org). But what you probably don’t know is that most of the time used to load an application is spent on disk accesses: firing up OpenOffice.org causes at least 130 MB of mostly small files to be read from the disk, and those disk accesses add up dramatically, mainly because of disk seek time.
You’ve also probably noticed that loading the same application a little while later takes a much shorter time. Why is that? It’s the cache who’s responsible of this big speedup: if Linux avoids reading files from disk, it runs much, much faster.
It’s also clear that having 22 MB available for a disk cache is just not enough to have OpenOffice.org start in less than 10 seconds, whether it’s the first time you’re starting it, or you just ran it a few moments ago and you’re running it again.
Most other everyday operations are affected. Opening folders in Nautilus or Konqueror are good examples.
So, how does swap actually enter the RAM equation here? What’s Linux not telling me?
It’s unbelievably simple, although not apparent at first.
Most running programs have code paths and data in memory that they rarely, if ever, touch. I would be bold and say most program-allocated memory is very rarely touched, but I might be wrong. So, the RAM taken up by unused code and data would actually be better utilized as cache for frequently-used files (or, even more beneficial, inode and dentry cache).
Having a swap partition gives Linux a space to put those rarely-used code paths and data segments on disk, freeing memory for the disk cache and new tasks. (Actually, shared libraries, unlike program data, can be evicted from RAM and re-read again from the shared library files when needed, but this incurs more disk seeks and computation effort than reading them back from swap, so if Linux has swap available for use, it’ll always prefer to put the shared libraries’ memory into swap).
The more memory available for the cache, the faster the response for frequently-performed tasks (like opening your huge MP3 folder… or my own huge MP3 folder, with Nautilus).
And that’s exactly why having swap is beneficial: unused code and data can be evicted from RAM, and that RAM can be put to better use. Even if you had over a gigabyte of physical RAM, that RAM would be better utilized as disk cache instead of being taken up by unused code. I’m not kidding: swap can make the difference between zero disk reads and three hundred disk reads.
This becomes more critical when low on RAM: imagine if the system were able to cache those 22 MB plus a 150-something MB of RAM, gotten by evicting unused stuff? Seen under that light, starting OpenOffice.org up is beginning to sound even doable, right?
There’s another reason, but it’s not performance-related. Some applications do allocate and consume a lot of memory. If you were to load several of those applications, the possibility of hitting zero free memory without swap is greater. And hitting zero memory is not good, because the system will kill processes it deems “memory hogs”. Best-case scenario: the application you just started dies. Worst-case scenario: a system daemon providing vital services (in my computer, it would be MythTV recording an episode of Nip/Tuck) dies. No one wants that!
In short: don’t skimp on swap. I do not believe in the 2.5X formula for swap (RAM times two point five). But I do keep twp half-a-gigabyte swap partitions (on different disks, in separate controllers, with equal priorities for added performance, and not the disk I use for MythTV) mounted at all times.
And how much RAM should you buy? As a general rule, if having all your commonly-used applications open causes the system to become slow (especially when opening the GNOME or KDE panel menu, or switching between applications), with applications appearing to “hang” for a couple of moments, that’s your cue to get more RAM: double it.
A faster processor won’t help at all, seeing as most of the time is spent on reading and writing to the disk swap partition. More swap won’t save you from this occurrence (indeed, it’s the accesses into swap which is making your applications “hang”), but not having it would be even worse for system performance (seeing as your system would still be short on memory, and it wouldn’t have a space to put unused data away).
What might help, if you’re short on money and long on free disk space, is setting up two swap partitions, each on a different disk connected to a different controller, and mounted with the same priority.
But remember that deciding to not be a cheap bastard and getting more RAM is the best course of action.
January 17th, 2006 at 23:54
The best idea IMO is to let the system adjust swap space as needed. I start my Linux box with no space set aside for swap but let it allocate swap on an as-needed basis in 64MB chunks. Works well by not slowing the system with swap you don’t really need or allowing the system to really grind to a halt by running out of memory.
January 18th, 2006 at 1:00
I see that you used Wordpress to generate the page. Kinda ironic but using Firefox 1.5 to view the page renders it almost unreadable while using IE it renders perfect. This is a blog about Linux too!!!
January 18th, 2006 at 1:01
“I start my Linux box with no space set aside for swap but let it allocate swap on an as-needed basis in 64MB chunks.”
Can you explain how to configure this?
Many thanks.
January 18th, 2006 at 12:10
Jim: it looks perfectly on my Firefox 1.0.7 in my FC4 deployment… what did look wrong?
January 18th, 2006 at 13:52
Jim: It also looks good here on both FC4 Firefox 1.5 desktop & OpenSuse 10 Firefox 1.5.
January 18th, 2006 at 14:02
It seems the layout problems are timing-related, basically when this server is feeding the HTML to your browser at a slow rate, and at certain, key, points, a burst of HTML comes, Firefox ends up laying out the sidebar wrongly, and then you see it “on top” of the content, or some other weirdness.
The validator at the W3C site does not seem to complain about this page, though.
Out of my control =(.
January 19th, 2006 at 3:16
If all this is true, I’d like to know, then why my machine, with 1GB of RAM & a half GB of swap, never shows any swap being used? Not ever.
I’m currently running with 630 MB of RAM used & no swap. 97 processes running.
Seem to me that unless you are doing video editing or have multiple memory hog apps open, that swap isn’t important with a gig of RAM.
Dave
January 19th, 2006 at 4:35
Is it possible to create a swap partition in the memory? I mean a virtual file system in the memory used as swap partition. It would use a very fast media and still the computer would have swap.
January 19th, 2006 at 6:18
For linux kernel 2.6.x you have setting in /proc/sys/vm/swappiness, telling the kernel how aggressive it should be regarding swapping. The default value on fedora this value is 60. Higher value means more swapping (I think the value ranges from 0 to 100).
On my desktop machine I use the command “echo 100 > /proc/sys/vm/swappiness” as root to tell the kernel to be very aggressive with swapping. This means that the kernel will swap out many unneeded “pages” from memory and instead use the memory for caching as described in the article. So on my machine the swap usage is high, but so is the memory usage for cache. A lower value means that the kernel will not try to swap as much to free memory for cache. Just try with different values here, since you can change this value runtime as shown above..
January 19th, 2006 at 13:38
I have 512MB RAM, and never felt the need for much swap, except when I’m running a memory intensive application like Mathematica, SCILAB, editing large images with GIMP or running VMware. Usually runs with ~350MB free RAM.
January 19th, 2006 at 16:14
Using RAM as a swap space would be extremely dumb, since the whole point of swap is to MAKE ROOM in fast RAM for cache, shared libraries and applications.
In other words, swap is used to simulate RAM. What do you gain from using a part of your RAM as swap? Nothing!
January 19th, 2006 at 16:15
If you have a gigabyte of RAM, and swap is never touched on your system, then you might as well disable the swap. Just remember to reenable it when you’re going to start memory-hungry applications…
…oh wait, why should you need to manually enable and disable swap, when leaving it on all the time has the same effect, plus you don’t need to intervene at all?
(that was a bit tongue-in-cheek)
August 22nd, 2007 at 0:33
[...] Why swap on Linux is always good, even with tons of RAM [...]