How to add Swap Space on Linux Servers (CentOS/RHEL) On the Fly
In Most of the UNIX-like operating systems, the swap partition is created during the installation time, However, with kernel 2.6 swap files can be used as a 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 a swap file as a dedicated partition.
Why Linux require Swap Memory?
Linux divides its physical RAM (random access memory) into chunks 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:
# dd if=/dev/zero of=/root/swapfile bs=1M count=1024
Step 2: Swap Area on swapfile using mkswap.
Step 3: Enable Swap space on the file.
Step 4: check the status of Swap space using below commands:
Step 5: To activate swapfile after reboot, add below entry to /etc/fstab file.
/root/swapfile swap swap defaults 0 0