How to add swap RAM

How to add swap RAM

If you find your machine running low on physical RAM, you can add swap ram.
Swap allows inactive pages to be moved from RAM to the swap space. Swap is space allocated from your hard disk quota and reserved for swap RAM usage.

You can either create a swap partition to used as swap RAM whereby the entire partition would be dedicated to swap.
Alternatively, you can also create a file which can be used to create swap RAM with. We will use a file today!

Follow these steps to add 1GB of swap to your server. If you want to add 5GB instead of 1 GB, replace 1G with 5G etc.

  1. Create a file which will be used for swap.   yum -y install fallocate fallocate -l 1G /swap
  2. Fix permissions. chmod 600 /swap
  3. Create swap space. mkswap /swap
  4. Enable swap. swapon /swap Now, we need to make the changes permanent.
    To do so, we need to edit the contents of /etc/fstab and add a new line: vi /etc/fstab /swap swap swap defaults 0 0
  5. Swap has now been added and we can check this. swapon --show

You can also remove the swap

  1.   swapoff -v /swap
  2. Remove the entry /swap swap swap defaults 0 0 from the /etc/fstab file.
  3. rm /swap