Manually Empty Linux Swap and Control Linux Swap Usage
I have noticed that the Linux versions of VMware products love their swap usage. I do not know if this is a “fault” of Linux or the VMware software but it annoys and slows me down. After looking around for an elegant solution I just decided to take the brute force method to emptying my swap: running sudo swapoff -a;sudo swapon -a
as (make sure you have enough free RAM to fit the contents of swap).
There is also the option of swappiness included with the 2.6.x+ kernel. Just edit /etc/sysctl.conf to include the line vm.swappiness=0
. Either change the existing value or, if it does not exist, add it (changing/adding this value will require a reboot or running sudo sysctl vm.swappiness=value
). vm.swappiness
can be between 0
and 100
(inclusive) where 0
will try to never swap anything and 100
will aggressively swap. If you would rather just change the value until next reboot use the line sysctl vm.swappiness=value
(replacing value with an appropriate value). You can view the current value with the command cat /proc/sys/vm/swappiness
. Ubuntu 10.04, for example, has a default value of 60
.
Update 2011.02.14
While searching for something completely unrelated I came across this from the Community Ubuntu Documentation:
#!/bin/bash
err="not enough RAM to write swap back, nothing done"
mem=`free|grep Mem:|awk '{print $4}'`
swap=`free|grep Swap:|awk '{print $3}'`
test $mem -lt $swap && echo -e $err && exit 1
swapoff -a && swapon -a
Stick it into a script, make it executable, and it will let you know if you have enough free RAM to empty the swap before doing it.