Skip to Content

Solved: How do I increase swap memory in CentOS 7 and CentOS 8

What is Swap Space?

Linux has the facility to divide physical RAM into tiny chunks of memory called Pages. Swapping is a method by which a page of memory can be copied to the pre-configured space on the hard disk which is called Swap Space, to free that particular page of memory. Thus, the combination of physical memory and the swap space is the actual amount of virtual memory available.

Content Summary

Use of Swap Space
Methods to increase swap space on CentOS 7
Swap Partition
Swap File
Extend swap space on Linux with swap file in CentOS 8
Conclusion

Use of Swap Space

Swapping is critical for two reasons:

  • If the system needs more memory than physically available memory, the kernel swaps out less utilized pages and provides memory to the current application(process) that requires the memory instantly.
  • A significant number of pages utilized by an application or process during the initial phase may only be used for initialization and then be never used again. The system shall swap out those pages and free the memory for other applications(process) or even the disk cache.

Methods to increase swap space on CentOS 7

There are two methods by which we can increase the swap space:

  • Swap Partition
  • Swap File

Let’s discuss each of the methods in detail.

Swap Partition

Prerequisites to check before creating swap partition

Step 1: Execute the below command to check the available swap space:

swapon -s

Execute the below command to check the available swap space: swapon -s

Step 2: Execute the below command to check the system overall space:

free -m

Execute the below command to check the system overall space: free -m

Step 3: Execute the below command to check the available space on the drive:

df -h

Execute the below command to check the available space on the drive: df -h

Create swap partition

Step 1: Execute the below command to create a partition for swap space:

fdisk /dev/sda

Step 2: Type n to proceed to create a new partition and press the Enter key to continue.

Step 3: Select which type of partition you want to create, p for Primary, or e for Extended). Here we will select the Primary partition. Type p and press the Enter key to continue.

Step 4: Enter the partition number, and press the Enter key to continue.

Step 5: Select the first sector and last sector to be Default, and press the Enter key to continue.

Execute the below command to create a partition for swap space.

Step 6: Type p to list the partition and press the Enter key to continue.

Step 7: Type l to list the partition ID and press the Enter key to continue.

Type p to list the partition and press the Enter key to continue. Type l to list the partition ID and press the Enter key to continue.

Step 8: Type t to change the partition ID and press the Enter key to continue.

Step 9: Select 82 for Linux Swap / So.

Step 10: Type p to list the created swap partition and press the Enter key to continue.

Step 11: Type w to save the command.

Type t to change the partition ID and press the Enter key to continue. Select 82 for Linux Swap / So. Type p to list the created swap partition and press the Enter key to continue. Type w to save the command.

Step 12: Execute the below command to refresh the system settings:

partprobe /dev/sda1

Execute the below command to refresh the system settings: partprobe /dev/sda1

Step 13: Execute the below command to format the created partition:

mkswap /dev/sda1

Execute the below command to format the created partition: mkswap /dev/sda1

Step 14: Execute the below command to activate the swap partition:

swapon /dev/sda1

Execute the below command to activate the swap partition: swapon /dev/sda1

Step 15: Execute the below command to verify the activated partition:

cat /proc/swaps

Execute the below command to verify the activated partition: cat /proc/swaps

Step 16: Execute the below command to add the newly created partition to fstab configuration file:

vim /etc/fstab

Execute the below command to add the newly created partition to fstab configuration file: vim /etc/fstab

Step 17: Save and exit the fstab page.

Step 18: Reboot the System.

Step 19: Execute the below command to check the created partition whether it is listed or not:

cat /proc/swaps

Execute the below command to check the created partition whether it is listed or not: cat /proc/swaps

Swap File

No additional hard drive is available in the system, create a file for use the swap space increase.

Create swap file

Step 1: Execute the below command to create a swap File:

dd if=/dev/zero of=/root/swap_file bs=1M count=1024

Execute the below command to create a swap File: dd if=/dev/zero of=/root/swap_file bs=1M count=1024

Step 2: Execute the below command to check the newly created swap File permission:

ls -l /root/swap_file

Execute the below command to check the newly created swap File permission: ls -l /root/swap_file

Step 3: Execute the below command to set a swap File permission:

chmod 600 /root/swap_file

Execute the below command to set a swap File permission: chmod 600 /root/swap_file

Step 4: Execute the below command to make the file as swap file:

mkswap /root/swap_file

Execute the below command to make the file as swap file: mkswap /root/swap_file

Step 5: Execute the below command to activate the newly created swap file:

swapon /root/swap_file

Execute the below command to activate the newly created swap file: swapon /root/swap_file

Step 6: Execute the below command to add a newly created swap file entry to fstab page:

vim /etc/fstab

Execute the below command to add a newly created swap file entry to fstab page: vim /etc/fstab

Execute the below command to add a newly created swap file entry to fstab page: vim /etc/fstab

Step 7: Save and exit the fstab page.

Step 8: Execute the below command to verify the newly created swap file in available:

swapon -s

Execute the below command to verify the newly created swap file in available: swapon -s

Step 9: Execute the below command to check the free memory:

free -m

Execute the below command to check the free memory: free -m

Extend swap space on Linux with swap file in CentOS 8

Step 1: Execute the below commands to find out the size of the existing swap space and partition

free -m
swapon -s

Execute the below commands to find out the size of the existing swap space and partition free -m swapon -s

A swap partition with a capacity of 4 GB is used in my case. So we will be extending swap space by 1 GB

Note: All of the below-mentioned commands require sudo privileges to make changes.

Step 2: Execute the below command if you like to create a file of size 1GB:, run the below command:

dd if=/dev/zero of=/root/swap_file bs=1G count=1

Execute the below command if you like to create a file of size 1GB:, run the below command: dd if=/dev/zero of=/root/swap_file bs=1G count=1

Note: As per your requirements, replace the values for bs and count. Keep in mind that bs refer to sets of block size and count refer to number of blocks.

Step 3: Execute the below command to create a file:

sudo fallocate -l 1G /swap_file

Execute the below command to create a file: sudo fallocate -l 1G /swap_file

Step 4: Execute the below command to set the permissions of the swap file to 600:

chmod 600 /swap_file

Execute the below command to set the permissions of the swap file to 600: chmod 600 /swap_file

Step 5: Execute the below command if the file does not have a swap area:

mkswap /swap_file

Execute the below command if the file does not have a swap area: mkswap /swap_file

Step 6: Add the below entry to vi editors to edit the fstab file:

Add the below entry to vi editors to edit the fstab file.

Step 7: Execute the below command to enable swap space on the file and which will eventually extend the swap space:

swapon /swap_file

Execute the below command to enable swap space on the file and which will eventually extend the swap space: swapon /swap_file

Step 8: Execute the below command to verify the swap space:

free -m
swapon -s

The swap space has been extended to 5 GB.

Execute the below command to verify the swap space: free -m swapon -s  The swap space has been extended to 5 GB.

Conclusion

Managing swap space is a critical aspect of system administration. With proper planning and usage, swapping can provide more benefits to Linux users.

Tags

Tags

    Ads Blocker Image Powered by Code Help Pro

    Ads Blocker Detected!!!

    This site depends on revenue from ad impressions to survive. If you find this site valuable, please consider disabling your ad blocker.