Clear Swap forcefully in Ubuntu !
Scribbled on Wednesday, February 4th, 2009If you are one of those guys who keep their PC running 24×7 then you must be among the same guys who end up getting a lot of swapped out memory. It can be a pain in the ass. But what if you can just dump down everything from swap to memory, provided your memory have that much free space. Yes it’s possible and is very advisable to do if you ever come to a situation like this.

I can surely think of dumping swap into RAM not used !
Here is the command to dump swap space to RAM while the system is running:
See what happens after executing this command, shown in below screenshots:

The process of dumping started :p

trrrrrrrrr……………

It’s complete !
- Or if you want to do it automatically using a shell script which checks that there is enough free RAM to dump
msg=”cannot write swap back to ram…nnot enough memory - bye…”
mem=`free|grep Mem:|awk ‘{print $4}’`
swap=`free|grep Swap:|awk ‘{print $3}’`
test $mem -lt $swap && echo -e $msg && exit 1
echo -e “noutput of free before:” &&
free &&
swapoff -a && swapon -a &&
echo -e “noutput of free after:” &&
free &&
exit 0
- Save the file as dumpswap.sh, provide execution privileges:
- Put the file in /usr/sbin
- Now you can always type this command to do the housekeeping
- That’s it !
References: http://wiki.archlinux.org/index.php/Empty_Swap | man swapon | man swapoff

No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.





6 Responses to “Clear Swap forcefully in Ubuntu !”
Did you have any problems caused by that amount of swap-usage, or did you just don’t like the look on that graph?
By THD on Thu 05th Feb, 2009
@THD
Well…it doesn’t matter much when I don’t use many programs simultaneously for long time, but after awhile it takes some time to swap out things (I use GIMP and do some JAVA dev too). So it is better to dump the swapped pages back to RAM all at once within a few seconds instead of getting irritated in bits.
There is one swappiness value which is associated with kernel too. You can change that but I didn’t find it of much use
/proc/sys/vm/swappiness
A better option in my opinion is to add another RAM module as a permanent solution and disable swap altogether from fstab.
I hope it helps !
By T on Thu 05th Feb, 2009
Hey…this is useful buddy. Can you tell out a way to do the same with windows…
By Tejaswini on Mon 09th Feb, 2009
Only thing I can think of is some third party cache manager in Windows.
By T on Mon 09th Feb, 2009
This was very useful for me while installing Windows XP on a virtual machine using VMWare Server 2. For whatever reason, the kernel decided to swap out about 400MB of data that VMWare was using even though there was over 900MB of RAM free. Disabled swap, and viola! Much faster now.
By Nathan crawford on Fri 24th Jul, 2009
Thank you!
By Mattia on Sat 03rd Oct, 2009