SWAP disk setup

Some VPS may not have a swap disk. It is useful when our server is running out of RAM to continue working properly. We must be careful, a swap disk may be useful to be online on peak users moments, it will start using the hard disk as extra RAM space and the hard disk is much slower than RAM, it would also start consuming more CPU moving data from and to RAM, so we can’t be continually depending on swap memory.

Here we find to kind of servers:

  • One, lets us assign new virtual drives to our hosting, so we can use a virtual drive to store swap memory (Amazon EC2 like)
  • The other, doesn’t let us new virtual drives in our hosting, so we must create a swap file (DigitalOcean).

I am using Ubuntu server 12.04 x64 for this guide.

How much space for swap?

There’s not a magic recipe to tell you how much space you must allocate for your server (in a swap disk o a swap file). It’s based on the common use of your server. If we have 4Gb of RAM we can start allocating 1Gb, or 2Gb. If, after a few days, everything’s running well, we can leave it, if it seems it’s not enough we can increase the swap space, but it would be time to think about a RAM increase. Of course this must be based on the average use of the server and, the cost (our worst enemy); if our swap space wasn’t enough on an eventual peak server load or everyday use.

Creating swap file

First of all, must make sure we have enough disk space on our server. We are creating a 2Gb swap file for our server. And we will locate it in /var/swap, so we can create a new 2Gb empty file doing:

$ sudo dd if=/dev/zero of=/var/swap bs=2048 count=1024k

1048576+0 records in
1048576+0 records out
2147483648 bytes (2.1 GB) copied, 8.07862 s, 266 MB/s

Once we have this created, we must set up our swap space, now it’s an empty file:

$ sudo mkswap /var/swap

Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=03bb7eb2-dcd7-40b6-bcf5-309836d448cc

Let’s activate our new swap file!

$ sudo swapon /var/swap

Creating swap on virtual disk

First, we must create a new virtual disk, it can be done in our provider’s control panel, so we will suppose it has already been added and located on /dev/xvdb3 . Then, we must make sure it’s a swap space (sometimes we can do it with that control panel), so we type:

$ sudo fdisk -l /dev/xvdb

Disk /dev/xvdb: 97642 MB, xxxxxxx bytes
255 heads, 63 sectors/track, xxxx cylinders
Units = cylinders of 16065 * 512 = xxxx bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1               x          xx      xxxxxxx+  83  Linux
/dev/xvdb2              xx         xxxx     xxxxxxxx   83  Linux
/dev/xvdb3             xxxx        xxxxx     xxxxxxxxxx   83  Linux

Our partition /dev/xvdb3 is not a swap space, but we can convert it executing (interactive text is written in bold):

$sudo fdisk /dev/xvdb3

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 82

Command (m for help): p

Disk /dev/xvdb: 97642 MB, xxxxxxx bytes
255 heads, 63 sectors/track, xxxx cylinders
Units = cylinders of 16065 * 512 = xxxx bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1              x          xx      xxxxxxx+  83  Linux
/dev/xvdb2             xx         xxxx     xxxxxxxx   83  Linux
/dev/xvdb3            xxxx        xxxxx     xxxxxxxxxx   83  Linux swap / Solaris

 

Command (m for help): w

We now have our swap disk drive, we can set up the swap disk and activate it:

$ mkswap /dev/xvdb3

Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=03bb7eb2-dcd7-40b6-bcf5-309836d448cc

 

$ swapon /dev/xvdb3

Making it permanent

To make the system activates the swap disk at boot time (sometimes, servers need rebooting), we must add a new line to /etc/fstab:

/dev/xvdb3              none                    swap    sw                      0       0

in case of swap partition or:

/var/swap              none                    swap    sw                      0       0

in case of swap file.

Checking our system is using swap space

To check our swap space we can type (tested in a 512Mb server):

$ free

             total       used       free     shared    buffers     cached
Mem:        503252     495444       7808          0       3108     394036
-/+ buffers/cache:      98300     404952
Swap:      2097148          0    2097148

or:

$ swapon -s

Filename                Type        Size    Used    Priority
/var/swap                               file        2097148    0    -1

We have successfully activated our new swap space. We must see less (or none) Out of memory (OOM) kills in our dmesg information, like I had on high usage moments, making some users impossible to see my site:

$ dmesg

[557354.896599] Out of memory: Kill process 25496 (apache2) score 21 or sacrifice child
[557354.897487] Killed process 25496 (apache2) total-vm:440892kB, anon-rss:55964kB, file-rss:3872kB
[557362.773956] apache2 invoked oom-killer: gfp_mask=0x200da, order=0, oom_adj=0, oom_score_adj=0
[557362.773962] apache2 cpuset=/ mems_allowed=0
[557362.773967] Pid: 25398, comm: apache2 Not tainted 3.2.0-23-virtual #36-Ubuntu
[557362.773970] Call Trace:
[557362.773982]  [] ? cpuset_print_task_mems_allowed+0x9d/0xb0
[557362.773990]  [] dump_header+0x91/0xe0
[557362.773995]  [] oom_kill_process+0x85/0xb0
[557362.773998]  [] out_of_memory+0xfa/0x220
[557362.774003]  [] __alloc_pages_nodemask+0x7ea/0x800
[557362.774010]  [] alloc_pages_vma+0x9a/0x150
.....

Leave a Reply

Your email address will not be published. Required fields are marked *