Create swap space on disk

My webserver doesn’t have a swap section installed by default. This is not a big deal as long as you don’t require a lot of memory. Running this website doesn’t require more than 300mb of ram. But sometimes I would like to run other tools or programs on this server as well that do require more memory than I have available.

I found this nice method to create a swap space on my disk. In the following example we create a swap space of 2GB. Create an empty file of exactly 2GB. Then set up the empty file as a swap space.

sudo dd if=/dev/zero of=/var/swap.img bs=2048k count=1000
sudo chmod 600 /var/swap.img
sudo mkswap /var/swap.img

Enable or disable the swap space can be done in the terminal. To enable the swap space run sudo swapon /var/swap.img. To disable the swap space run sudo swapoff /var/swap.img.

Leave a Reply

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