How to add swap memory on Linux

Here were are going to create a 4GB SWAP memory. You can do this buy specifying a block size of 1GB with a count of 4. You can specify your own swap size.

sudo dd if=/dev/zero of=/swapfile bs=1G count=4

Enabling the Swap File
Make sure the file has proper permission.

sudo chmod 600 /swapfile

Now format the file so that operating system can understand this is a swap file .

sudo mkswap /swapfile

Now enable the swap memory

sudo swapon /swapfile