ADD Swap Space on Linux Servers (CentOS / RHEL) On the Fly


Overview :

In Most of the UNIX like operating System swap partition is created  during the installation time , However with the kernel 2.6 swap files can be used as swap partition.

The administrative flexibility of swap files outweighs that of partitions; since modern high capacity hard drives can remap physical sectors, no partition is guaranteed to be contiguous. We can add swap file as a dedicated partition.

Why Linux require Swap Memory…. ?

Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the pre-configured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Steps To Add Swap File

Step:1 Create a file(1GB) using dd Command 

Login into the terminal as a root , Type below command

[root@site1 ~]# dd if=/dev/zero of=/root/swapfile bs=1M count=1024

Step:2 Swap Area on swapfile  using mkswap

[root@site1 ~]# mkswap  /root/swapfile

Step:3 Enable Swap space on the file.

 [root@site1 ~]# swapon /root/swapfile

Step:4 check the status of Swap space using below commands

 [root@site1 ~]# swapon -s




Step:5 To activate swapfile after reboot, add below entry to /etc/fstab file. 

/root/swapfile           swap                   swap    defaults        0 0