Wednesday, November 26, 2014

Loose the SWAP If You Got the RAM!

Swap space is hard disc space that is used as extra RAM (random access memory) when the operating system decides it's necessary. It allows systems with low system resources to run more programs while still exceeding their RAM capacity. While that is great and all, if you have 4 + gigs of RAM and don't do anything ridiculously resource intensive like video rendering, then swap may simply be slowing you down. Unless you are lucky enough to have a solid state drive, than read/writing to a hard disc every time you call upon a program will seriously slow your system down.

On Windows systems, swap is called "page-file" or something, but it is the same concept as outlined above. I'm sure there is some way to do this in a win system, but I am not going to get into it here. If you are running Linux, then you can do a a couple very simple things to speed your system up:

Only do this if you can afford to; you should have descent system specs (4 gigs of ram and an intel core2duo is what I'm on right now). If you have less, say 2 gigs of RAM, you probably should take precautions if you attempt to tweak this:

First, let's check your swap level:


$ cat /proc/sys/vm/swappiness

You can instruct your system to use a swap less aggressively, by executing (as root) the following comamand:


# sysctl vm.swappiness=0

For whatever reason Debian systems have the vmswappiness set to 60, which is a pretty swap-happy number (on a scale from 0 to 100), so this will speed your system up considerably. If you want to make the change permanent, open /etc/sysctl.conf as root, and insert the following line into the file:


# vm.swappiness = $n
Where $n represents an integer (whole number) from 0 to 100 (I use 0) which controls how swap-happy your system gets. Even with my swapiness set to 0, occaisonally when running a lot of RAM consuming tasks (like virtual machines), my system will start swapping everything anyway, even though I have plenty of RAM. You can check your system monitor to see how much swap is in use, if any is in use, and you do not have a solid state drive, (as in my case), you can issue the following commands (as root) to disable & re-enable the swap, which also loads anything in swap to RAM and gives you a clean slate:

# swapon -a && swapoff -a
# swapoff -a && swapon -a
 (oops)

Or you can simply issue swapoff -a to disable it completely. To make things a little easier, I wrote this simple bash script to clear the swap in these situations. Place this in your $path (generally /usr/local/bin/, unless you exported another path... more on that some other time) :

#!/bin/bash
echo "I will now empty the Swap..."
if  swapoff -a && swapon a ; then
:
else
 echo "#    ! ERROR !    #"
 echo  'Damn bro, That did not work. Got root?' >&2
   exit 1
fi
If all goes well, you won't get the error message, otherwise you will (duh). This script also must be ran as root, as it needs administrative privileges. Here is one example of something that could go wrong:

root@linuxpc:~# reswap
I will now empty the Swap...
swapon: a: stat failed: No such file or directory
#    ! ERROR !    #
Damn bro, That did not work. Got root?
Since I am using an encrypted /home partition, my swap file is also encrypted. As we all know, security often comes at the price of convenience. Once in a blue moon, the system will initially fail to find the swap partition (due to linux hiding/obscuring the partition labels and whatnot), but it usually corrects itself after a little while. However, this will work flawlessly most of the time.

In conclusion, solid state drives are badass, and if you don't have one, but have enough RAM, kill the swap already!

No comments:

Post a Comment