Enabing Swap Space on Linux VPS System
Experience of several Linux VPS providers has shown that several companies do not enable swap space on their VPS servers. I consider Xen based servers to be the best, so I always go with Xen providers. This may be something that is particular to Xen servers, however I canít say for sure.
As most people know, swap space is important on any system. It provides a buffer space when physical RAM is full, thus preventing (or at least reducing the number of) out of memory errors you may get.
Enabling Swap Partition
I have configured numerous Linux Xen VPS servers from hosts around the world, and it seems that most provide swap space, but some fail to configure it. On such servers, configuration is straight forward, simply login as root and edit /etc/fstab entering the following line:
/dev/sda2 swap swap defaults,noatime 0 0
Now reboot the server and the swap partition should be mounted and be used. Simple!
No Swap Partition?
In some cases, however, the swap partition may not be available. In such cases you will need to create a swap file. Swap files are files that exist on the main partition and work in the same way as the swap partition. The disadvantage here is that the swap file will take up space on you disk space quota, and swap files arenít always as quick to access as swap partitions.
Creating a Swap File
To create a swap file, login as root and enter the following command:
dd if=/dev/zero of=/swap bs=1024 count=1048576
This will create an empty file of 1048576 bytes (or 1Gb), you can adjust this count setting to the size you want. Click here to understand the relationship between bytes and gigabytes. As a rule of thumb use double the physical memory; this is usually a good compromise between too much and too little swap space (optimising the amount of swap space falls outside the scope of this article).
Now set the swap area with the following command:
and set itís permissions:
chmod 0600 /swap
Now edit /etc/fstab to enable to swap file on boot by adding the following line:
/swap swap swap defaults,noatime 0 0
Reboot the server and the swap file should now be working!