How to Add Swap on CentOS 6

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 pre-configured 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/hda              20642428   1347968  18245884   7% /

The swap file would take up 512MB and we are using only 7% of the /dev/hda 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=512k

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 = 536866 kB

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          524280 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          swap            swap    defaults        0 0

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

chown root:root /swapfile
chmod 0600 /swapfile

 

Configuring Swappiness

 

Swappiness 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.

To find out the current swappiness setting, you need to type in:

cat /proc/sys/vm/swappiness

To adjust the swappiness, you can make use of the sysctl command:

sysctl vm.swappiness=10
vm.swappiness=10

For a quick verification, you can try using the command again:

cat /proc/sys/vm/swappiness
10

In order to make sure that the VPS automatically apply these settings while booting, you can update the

/etc/sysctl.conf file:

sudo nano /etc/sysctl.conf
# Search for the vm.swappiness setting.  Uncomment and change it as necessary.
    vm.swappiness=10

You are done with setting up swap on your server.

 

support2 has written 111 articles

Leave a Reply