Wednesday, November 26, 2008

Clear Swap Space in Linux

There have been times where it has been necessary for me to clear out the swap space on a Linux system. In order to do this, you must first make sure that you have enough free memory to hold what is being used by swap.
I usually do this in a few steps. You do need to be root to do this. First I want to see what is currently being used.
free

Then I run the actual commands that empty the swap:
swapoff -a && swapon -a

Then I check what is being used after doing this.
free

I have these three steps in a script and then I just run the script, but, the commands could easily be run from the command line.

13 comments:

Волошин Руслан said...

Thanx it help for me

Anonymous said...

Thanks. I had puzzling situation with 4G RAM mostly free but a whole G of swap in use.
Your command line cleared swap although I'm still a bit puzzled.
top shows Swap: 2031608k total, 0k used, 2031608k free, 2031472k cached

What is that cached swap?

colymoore@gmail.com

Anonymous said...

Yes! Did exactly what I was wanting to do.

Anonymous said...

yup...really good advice...only that I don't think you really need the exact amount of free RAM as the amount of occupied swap...

I had 700 MB of used RAM out of 2 GB and 1.3 GB of swap. after using your commands I have 1.3 GB of RAM used and no swap :P

Thank you anyway

Duber said...

Thankkkkkkks, just what I was looking for.

Unknown said...

Thanks Dude. Short and sweet. Did exactly what I needed. Much Appreciated!

Weird World News said...

Rather use:

sync; echo 3 > /proc/sys/vm/drop_caches

It is better to sync before messing with cache

Anonymous said...

Thanks I was exactly looking for this.

Burhan

Nasimuddin Ansari said...

Below will decrease number incached colum and increased number in fee Mem colum. No effect for swap usages.

sync; echo 3 > /proc/sys/vm/drop_caches

[root@uksysops004 ~]# free -to
total used free shared buffers cached
Mem: 4046272 3924208 122064 0 334268 967444
Swap: 8191992 1499496 6692496
Total: 12238264 5423704 6814560
# sync; echo 3 > /proc/sys/vm/drop_caches
# free -to
total used free shared buffers cached
Mem: 4046272 2458000 1588272 0 404 59940
Swap: 8191992 1499496 6692496
Total: 12238264 3957496 8280768
#

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

The best super all in one clear cache, buffers and swap file in one go:

sync && sudo /sbin/sysctl vm.drop_caches=3 && sudo swapoff -a && sudo swapon -a

You might need to alter the permissions on the sysctl or vm.drop_caches file, or run the following command under su/root:

sync && /sbin/sysctl vm.drop_caches=3 && swapoff -a && swapon -a

Works flawless, every time :)

-Jamie M.

Curt- said...

Also, /proc/sys/vm/swappiness by default is 60, with 100 being aggressive swapping, 0 being "use swap only when actually running out of RAM"

Nice quick article. Agreed about sync first, though. Just in case.