How to Add Swap on Ubuntu 12.04

Introduction

 

Linux divides its RAM into chunks of memory known as pages. Linux swapping is a process for freeing up its pages of memory. When a Linux swap occurs, a page of memory will be copied from RAM to a preconfigured space on hard disk known as swap space.

The swapping does come with a few drawbacks. Since hard disk have a slower memory than RAM, the performance of the virtual private server will come down considerably. Also, swap thrashing can occur if too many pages are being swapped in and out.

 

Check for Enabled Swap Files

 

Before proceeding to set up swap on your system, let’s check if there are any files with enabled swap usage. You can get a list of such files by typing the following command:

sudo swapon -s

You will get an empty list that confirms that swapping is not enabled. Your result should look like this:

Filename                   Type          Size   Used   Priority

 

File System Check

 

First, we need to check how much space is available on the server. You can get that by using the df command:

 

df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda        20907056 1437188  18421292   8% /
udev              121588       4    121584   1% /dev
tmpfs              49752     208     49544   1% /run
none                5120       0      5120   0% /run/lock
none              124372       0    124372   0% /run/shm

 

The swap file would take up 256MB and we are using only 8% of the /dev/sda as shown above. Hence there wouldn’t be any issues with that.

 

Setting Up the Swap File

 

Let’s go ahead and create the swap file using the command given below:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k

Here, I have given the name ‘swapfile’.

Now you have to create a linux swap area:

sudo mkswap /swapfile

You will get an output similar to this on your screen:

Setting up swapspace version 1, size = 262140 KiB
no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb

Now, you can go ahead and activate the swap file:

sudo swapon /swapfile

 

Once it is done, you can check the swap summary as shown below:

swapon -s
Filename                          Type          Size   Used   Priority
/swapfile                               file          262140 0      -
1

This file will remain on your virtual private server until it reboots. You can make sure that the swap is permanent by updating that to fstab file.

Open up the /etc/fstab file:

sudo nano /etc/fstab

Add the following line to it:

/swapfile       none    swap    sw      0       0

Next step is to set the ‘swappiness’. It is a parameter to tweak the way Linux swaps. It can be a number from 0 to 100. Higher values will lead to more pages being swapped and lower values will leave more pages in memory.

In this tutorial, we will set swappiness to 10. If you skip this step, it may hinder the system performance. If swappiness is set to 10, it will act as an emergency buffer that prevents out-of-memory crashes in future.

Follow the steps to set swappiness:

echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

You will have to set correct permissions to the swap file:

sudo chown root:root /swapfile 
sudo chmod 0600 /swapfile

You are done with setting up swap on your server.

support2 has written 111 articles

Leave a Reply