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.
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.
There are two methods by which we can increase the swap space:
1. swap partition
2. swap File
First method : Swap Partition
check the available swap space use this command “ swapon -s “
check the system overall space use this following use the command “ free -m “
before allocating swap, check the space on availability of drive use the command “ df -h “
Now Create a partition for swap space using the following command “ fdisk /dev/sda “
Type “ n “ for New partition created command……..press the enter.
Select which type of partition you want ( Primary, Extended ). Now that we have selected the primary Partition, Press enter & select a Partition Number press enter.
Select the First sector and last sector to be Default………….. Press enter
List the Partition command “ p “
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 use command “partprobe /dev/sda1”
“mkswap /dev/sda1” Use command to format the created partition
“Swapon /dev/sda1”type command swap partition to activated.
“Cat /proc/swaps” use 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 to be listed or Not use command “cat /proc/swaps “
Second Method: Using File
No additional hard drive available in system, create a file for use the swap space increase.
Use the command 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 use 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 use command ” swapon -s ”
and “ free -m “check the free memory also.
Managing swap space is a critical aspect of system administration. With proper planning and usage, swapping can provide more benefits to Linux users.