How to create SWAP Space in RHEL/CentOS Linux Systems

Increasing swap space in RHEL/CentOS Linux systems is a common administrative task.

Swap space, often simply called 'swap' is a designated area on a storage device(like a hard disk or SSD) that is used as an extension of the system's RAM (Random Access Memory). When the physical RAM on a system is full, the operating system begins moving inactive pages from RAM to the swap space, allowing new operations to continue without crashing due to lack of memory.

The swap space can be implemented using a dedicated partition or a swap file. 

In modern systems, especially where resizing partitions is cumbersome or risky, using a swap file can be more flexible.

Here's a step-by-step guide to increase swap space in RHEL/CentOS Linux systems using a swap file.


1. Check the current swap space

Use the 'free' command or 'swapon --show' to check current swap usage.

$ free -h 

or

$ swapon --show 


2. Decide on Swap File Size

Determine how much additional swap space you want to add. For this example, let's say we want to add an additional 4GB of swap.


3. Create a Swap File

Use the 'fallocate' command to create a swap file. For instance, to create a 4GB swap file use following command

$ sudo fallocate -l 4GB /swapfile 

If 'fallocate' is not available or gives an error, you can also use 'dd'

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 


4. Set correct Permissions

It's crucial to set the right permissions to ensure that only root can read/write to the swap file.

$ sudo chmod 600 /swapfile 


5. Make the File as Swap

Convert the file to swap format

$ sudo mkswap /swapfile 


6. Enable the Swap File

Once the file has been set up, you can enable it

$ sudo swapon /swapfile 


7. Make the swap File Permanent

To ensure the system uses the swap file after reboot, add the swap file to the '/etc/fstab' file.

Open the '/etc/fstab' in your preferred editor (I like to use vim here)

$ sudo vim /etc/fstab 

Then, add the following line at the end

/swapfile     swap    swap    defaults    0    0 


8. Verify the swap space

Check that the swap is now available as provided

$ free -h 

or 

$ swapon --show 


That's it! Now you've successfully increased the swap space in your Linux System based on RHEL/CentOS distributions using a swap file. 


Why do we need Swap Space?

Memory Overflow Buffer:- 

Even with the abundance of RAM in modern systems, there are times when systems may exhaust it. In such cases, the operating system uses swap space as an overflow buffer to ensure processes can continue running.

Inactive Processes:-

It's efficient for a system to move infrequently accessed memory pages to swap space, freeing up RAM for active tasks.

Hibernate Function:-

For laptops, the hibernate function (Which saves the RAM state to the disk and powers off) uses swap spaces to store the content of the RAM.


How much Swap Space should you Allocate?

Historically, a general recommendation was to allocate twice the amount of RAM as swap. However, with the increase in system RAM in modern machines, this is often unnecessary.


Swap Space considerations

Performance:- 

Swap space is significantly slower than RAM. Over-reliance on swap can degrade system performance.

SSD and Lifespan:-

Constant writes to an SSD (which might happen if you're heavily using swap) can reduce the lifespan of the SSD over time.

Multiple Swap spaces:- 

It's possible to have multiple swap spaces (like swap partitions or swap file). The OS can manage and use them efficiently.


Final Thoughts:-

While swap space can be a lifesaver in situations with limited RAM, its always best to monitor system resources and add more RAM if frequently running out. Swap is tool for flexibilty and stability, not a substitute for adequate memory.

Remember, the use of swap space as a replacement for RAM can negatively impact system performance. 

It's always better to add more physical RAM if possible. However, having sufficient swap can prevent system crashes in scenarios where RAM is exhausted.