Table of Contents
- What is Swap Space?
- Use of Swap Space
- Methods to Increase Swap Space on CentOS 7
- Method 1 – Swap Partition
- Method 2 – Swap File
- Steps for extending Swap Space on Linux with a Swap File in CentOS 8
- 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.
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
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.
Method 1 – Swap Partition
Prerequisites to check before creating swap partition
- Check the available swap space using the command below:
- Check the system overall space using the command below:
- Check the available space on the drive, using the command below:
“ swapon -s “
“ free -m “
“ df -h “
Creating swap partition:
- Create a partition for swap space using the following command below:
- Type “ n “ to proceed to create a new partition and press Enter
- Select which type of partition you want (Primary, Extended). Here we will select the Primary Partition. Type p and press Enter
- Then enter the partition number, and press Enter
- Then, select the first sector and last sector to be Default………….. Press Enter
You can list the Partitions using the command “ p “
Similarly, Press “ l “ to listed the below ID
press ” t ” to change the partition ID and select 82 for Linux Swap / So
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”
“mkswap /dev/sda1” Use this command to format the created partition
“Swapon /dev/sda1” type this command to activate the swap partition
“Cat /proc/swaps” use this command to verify the activated partition to below
“Vim /etc/fstab” Type the command to add the newly created partition to fstab configuration file
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 “
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.
Check the newly created swap File permission using the command ‘ “ ls -l /root/vembuswap “
Set a swap File permission use command ” chmod 600 /root/vembuswap “
Make the file as swap file using command “mkswap /root/vembuswap ”
Activate the newly created swap file command “swapon /root/vembuswap “
Add a newly created swap file entry to fstab page use command ” vim /etc/fstab ”
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.
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’
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:
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.
Set the permissions of the swap file to ‘600’ by running the chmod command on it.
If the file does not have a swap area, run the mkswap command.
In the fstab file, add the below entry to vi editors to edit the fstab file.
Run the swapon command to enable swap space on the file and which will eventually extend the swap space,
After that, you can run the below commands to verify the swap space.
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.