How to add a swap partition

I like to use DigitalOcean for trying out new web-applications and scripts. The cheapest plan comes at $5/month but provides only 512MB of memory. Adding a 1Gb swap memory is usually sufficient for temporary memory extensive needs. These is a short documentation on how to add the swap partition.

Adding a swap partition:

sudo dd if=/dev/zero of=/swap bs=1M count=1024
sudo mkswap /swap
sudo swapon /swap

Making the partition persistent on system reboots:

sudo vi /etc/fstab
# Add a line to tell the system to use the file
/swap   none    swap    sw  0   0

Optimize swappiness and vfc_cache_pressure for VPS needs:

sudo vi /etc/sysctl.conf
# Add the following at the bottom
vm.vfs_cache_pressure = 50
vm.swappiness = 10

The detailed article about what each of the above commands do is available here.

Pratyush Mittal @pratyush