Table of Contents

  1. What is Swap Space?
  2. Use of Swap Space
  3. Methods to Increase Swap Space on CentOS 7
  4. Method 1 – Swap Partition
  5. Method 2 – Swap File
  6. Steps for extending Swap Space on Linux with a Swap File in CentOS 8
  7. Conclusion

What is Swap Space?

Linux has the facility to divide its physical RAM into tiny chunks of memory called as 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.

Protect Your Data with BDRSuite

Cost-Effective Backup Solution for VMs, Servers, Endpoints, Cloud VMs & SaaS applications. Supports On-Premise, Remote, Hybrid and Cloud Backup, including Disaster Recovery, Ransomware Defense & more!

Use of Swap Space

Swapping is critical for two obvious reasons.

  • First, 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
  • Next, a significant number of pages utilized by an application or process during its 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

Download Banner

Methods to Increase Swap Space on CentOS 7

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

  1. Swap Partition
  2. Swap File

Let’s discuss each of the methods in detail.

Method 1 – Swap Partition

Prerequisites to check before creating swap partition

  1. Check the available swap space using the command below:
  2. swapon -s

    swap memory in Centos

  3. Check the system overall space using the command below:
  4. free -m

    swap memory in Centos

  5. Check the available space on the drive, using the command below:
  6. df -h

    swap memory in Centos

Creating swap partition:

  1. Create a partition for swap space using the following command below:
  2. swap memory in Centos

  3. Type “ n “ to proceed to create a new partition and press Enter
  4. Select which type of partition you want (Primary, Extended). Here we will select the Primary Partition. Type p and press Enter
  5. Then enter the partition number, and press Enter
  6. Then, select the first sector and last sector to be Default………….. Press Enter

You can list the Partitions using the command “ p

swap memory in Centos

Similarly, Press “ l “ to listed the below ID

press ” t ” to change the partition ID and select 82 for Linux Swap / So

swap memory in Centos

Command (m for help) “ p “ below the created swap partition to be listed.

press ” w ” to save command.

Refresh the system settings using the command “partprobe /dev/sda1

swap memory in Centos

mkswap /dev/sda1” Use this command to format the created partition

swap memory in Centos

Swapon /dev/sda1” type this command to activate the swap partition

swap memory in Centos

Cat /proc/swaps” use this command to verify the activated partition to below

swap memory in Centos

Vim /etc/fstab” Type the command to add the newly created partition to fstab configuration file

swap memory in Centos

Save and exit the fstab page.

Reboot the System.

After reboot, check the created partition whether it is listed or not using the command “cat /proc/swaps

swap memory in Centos

Method 2 – Swap File

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

Creating swap file:

Use the command below to create a swap File
dd if=/dev/zero of=/root/vembuswap bs=1M count=1024 “ and press the enter.

swap memory in Centos

Check the newly created swap File permission using the command ‘ “ ls -l /root/vembuswap

swap memory in Centos

Set a swap File permission use command ” chmod 600 /root/vembuswap

swap memory in Centos

Make the file as swap file using command “mkswap /root/vembuswap

swap memory in Centos

Activate the newly created swap file command “swapon /root/vembuswap

swap memory in Centos

Add a newly created swap file entry to fstab page use command ” vim /etc/fstab

swap memory in Centos

swap memory in Centos

Once add the fstab page Save & exit

Verify the newly created swap file in available using the command ” swapon -s
Use the command: “ free -m “check the free memory also.

swap memory in Centos

swap memory in Centos

Steps for extending Swap Space on Linux with a Swap File in CentOS 8

We first need to find out the size of the existing swap space and partition using the commands ‘free -m’ and ‘swapon -s’

swap memory in Centos

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 that all of the below-mentioned commands require sudo privileges to make changes)

If you like to create a file of size 1GB, run the below command:

swap memory in Centos

As per your requirements, replace the values for ‘ bs ‘ and ‘ count ‘.

Keep in mind:
bs 🡪 sets of block size
count 🡪 number of blocks

You can also use the fallocate command to create a file, as below command.

swap memory in Centos

Set the permissions of the swap file to ‘600’ by running the chmod command on it.

swap memory in Centos

If the file does not have a swap area, run the mkswap command.

swap memory in Centos

In the fstab file, add the below entry to vi editors to edit the fstab file.

swap memory in Centos

Run the swapon command to enable swap space on the file and which will eventually extend the swap space,

swap memory in Centos

After that, you can run the below commands to verify the swap space.

swap memory in Centos

So, when we checked before, the swap space was 4 GB. And now it 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.

Embark on an incredible journey with the amazing Linux Backup Solution!

Secure your vital data with the magic of Linux backup—a robust shield for safeguarding your valuable information. Try it for free!

Ready to start your adventure? Download BDRSuite now and shield your Linux world with ease, bringing tranquility.

Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.

3.7/5 - (6 votes)