What is swap space (memory) and how to use it

Swap memory is basically parts/pages of memory from the RAM (your high-speed primary memory) which is switching to use a file (or partition) dedicated on the harddrive as memory space instead. So if the physical memory is full, you can use swap space for extra memory resources. It is useful if you have low memory on your machine, and if you do not want your machine to run out of memory in eg. high traffic environments.

Swap memory from the harddrive is slower than the RAM, so be careful as it can have a lot of influence on the performance on your machine. This is why i set the swappiness to 0 - a bit more on that later.

You can see if you already have swap memory assigned by running (some Linux solutions got swap straight out of the box):
$ swapon -s

You can also enter for example $ top or, if installed, $ htop (see previous blog post) to see it in action.

If you do not already have swap on your machine, the first step is to create a file dedicated to the new swap memory space. You can do this with:
$ sudo dd if=/dev/zero of=/swap bs=1024 count=1024k

What this line does is that it create a file with input from /dev/null, and write it to the file "swap" at the location /. It writes to "swap" with 1024 bytes at a time (blocksize) 1024k (1024000) times (the count argument) until we have a file with the size 1024MB. It is a good procedure to make your swap twice as big as your physcial RAM (primary memory). In this case we are on a small 512MB RAM box, so we will make the swap memory twice as big with 1024MB.

The pseudo machine file /dev/zero is used to create a file with no data, but with a defined size like here. It writes as many nulls as we need. So everytime it is called we send a 0 to it, which will end up as a file with our desired size.

Do not get mistaken with /dev/null. /dev/null is mostly used to send output to it, and to get no output in return. This is useful for eg. cronjobs.

When this command has been executed the file should have been created, and you should see some output like this:

1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 3.87888 s, 277 MB/s

You can go to it's location (/) and run $ ls -l to get some details about the files including your new file "swap". Check if the size is correct.

If everything seems fine, make your newly created file (/swap) a swap file, with the mkswap (make swap) command:
$ mkswap /swap

Now activate your new swap memory file with swapon! This should do the trick:
$ swapon /swap

Now swap memory should be running on your machine - it is not more complex than this.

Confirm it by checking the swapon summary:
$ swapon -s

You should see something like:

  Filename ...
  /swap ...

Otherwise check it in action with top or htop (notice the Swp in the top left corner):

- htop

You can set the swappiness value in the file at /proc/sys/vm/swappiness:
$ echo 0 > /proc/sys/vm/swappiness

I set my swappiness to 0, as i do not want my machine to use swap if it is not an emergency. This is due to the low perfomance of harddisk memory compared to RAM.

To make all your new swap configuration permanent, as it usually resets after reboot, add the swap memory to your /etc/fstab configuration. Read more about fstab here: http://en.wikipedia.org/wiki/Fstab.

You could make all this we have done here as a simple bash script (will maybe make a optimized version later on, as this version is way too loose for eg. production):

#!/usr/bin/env bash 

# Get list of swap memory 
swapon -s

# Create file for swap memory
sudo dd if=/dev/zero of=/swap bs=1024 count=1024k

# Make this file a swap file
mkswap /swap

# Activate this swap file as swap memory
swapon /swap

# Get list of swap memory to see result
swapon -s

# Set your swappiness to 0 for perfomance fix
echo 0 > /proc/sys/vm/swappiness

Read more about swappiness here:

Swappiness (source: Wikipedia)

Swappiness is a property of the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space. The default value is 60, and for most desktop systems, setting it to 100 may affect the overall performance, whereas setting it lower (even 0) may improve interactivity (by decreasing response latency.)

vm.swappiness = 0
The kernel will swap only to avoid an out of memory condition.

vm.swappiness = 60
The default value.

vm.swappiness = 100
The kernel will swap aggressively which may affect over all performance.


TLDP also got a bit of documentation on Swap here:
http://www.tldp.org/HOWTO/Partition/settingupswap.html