Wednesday, April 15, 2009

Swappiness on Linux

Swappiness is a Linux kernel setting that controls how often your swap is used. For the most part there is no need to tweak this setting, the default values are fine. However under some circumstances, especially on laptops or low memory systems, you may want to adjust it.

The value could be set from anywhere from 0, no swappiness, to 100, swap as much out as you can. Different distributions set this differently. On Ubuntu and CentOS, the default is 60. At a bare minimum you should set it to 10, since a setting of 0 could lead to a system lockup if you run out of RAM.

For example, I'm currently using two laptops. One of my laptops is a Compaq Presario 700. It has a 500 mgz Athlon processor, and its motherboard supports a maximum of 384 megs of PC100 RAM. Older machine, and it still runs perfectly after about 8 years of use. It has a bare minimum installation of Ubuntu with the XFCE4 desktop environment. I want to use Firefox, which on a machine like this can take over the system pretty quick. I put the following in /etc/rc.local:

echo 100 > /proc/sys/vm/swappiness

That will cause everything in the background to get out of Firefox's way when it needs the RAM. When switching applications the desktop doesn't snap very well, since it has to retrieve code from swap a lot more, but it does give Firefox and other memory intensive applications the resources it needs to preform well. In fact, for the most part the machine seems to run all around better with swappiness turned all the way up. Of course, this will kill the battery life, but the battery failed years ago, so it really doesn't matter.

On the flip side, my second laptop is a Acer Aspire Gemstone with 1.7 Ghz processor with 2 gigs of RAM. I have a similar installation of bare minimum Ubuntu plus XFCE4. Again, I use Firefox and some office apps, however since I'm using such a lightweight desktop I'm hard pressed to ever make it past 1 gig of RAM. In fact, the last time I swapped out was when I was using VirtualBox and Firefox leaked memory due to a bug on a Flash based web page, but that was a long time ago. That was also before upgrading from 1 to 2 gigs of RAM. Since I have more than enough memory than I'll ever use, I set the following in /etc/rc.local:

echo 0 > /proc/sys/vm/swappiness

This is a dangerous setting, so I make sure I have a memory monitoring app on my panel. If I ever run out of RAM, which I doubt under the current setup, the machine will lock, and lock hard. The benefits however are tremendous. There should be never a reason to swap out, and when running on battery an errant spin up due to unnecessary swapping in and out could possibly lead to a loss of five to ten minutes of battery life. Any road warrior will tell you that 10 minutes on battery might as well be an hour.

You can adjust this setting on the fly as well. All you have to do is echo your value to the swappiness file just like in the rc.local examples above. For desktop machines with a gig of ram or more, most likely you won't see much of a difference no matter what you set it too, but it could be a handy tool for laptops with limited, or plenty, of resources.

No comments:

Post a Comment