Handling Swap

Here’s a neat tip I picked up from Andrew. Have you ever run into a machine that won’t reclaim all of its swap? It turns out there’s an easy way to force a reclaim.

First, a caveat. For most people, this makes no sense. If something is just sitting in swap even when there’s lots of free RAM, it means that whatever’s in swap is something that nothing, anywhere, cares about. It would have been reclaimed if anything tried to use it. You want to leave it in swap. And a second caveat: what I’m about to say is an awful idea if you don’t have lots of free RAM. But in cases where you’re using 4% swap and getting paged every 15 minutes because the monitoring system sees swap being used, and yet you have 5GB of free RAM, here’s a neat trick that will force everything in memory to be reclaimed.

First, find the swap partition. On Linux, it’s “cat /proc/swaps,” though you could also look at fstab. GNOME and KDE users probably have a fancy GUI for this, but our servers don’t run GNOME. 😉 On the machine I just did this on, it was /dev/sda2, so I’ll use that as an example.

Then, you ‘unmount’ the swap partition, and then re-enable it. As you turn off the swap partition, its contents are forced back into memory. “swapoff /dev/sda2” to turn it off, and then “swapon /dev/sda2” to turn it back on.

Leave a Reply

Your email address will not be published. Required fields are marked *