Swap memory, also known as swap space, is a form of virtual memory in Linux systems. It is used to extend the amount of physical memory (RAM) available to the system. When the system runs out of available RAM, the kernel will move some of the data from the RAM to the swap space on the hard drive. This process is known as swapping.
The swap space is typically located on a dedicated partition on the hard drive, but it can also be located in a separate file. The swap space can be used to temporarily store data that is not being used by the system, allowing the system to continue running even when physical memory is low.
When the system needs the data that was moved to swap space, the kernel will move it back to the RAM, allowing the system to access it more quickly. This process is known as swapping back.
The size of swap space is typically set to a value between 2 and 4 times the size of the RAM. The exact value depends on the amount of RAM in the system and the expected usage patterns.
Swap memory has some drawbacks, as access to the swap space is slower than access to RAM, and swap space can fill up quickly if the system is running low on RAM for an extended period of time. This can cause the system to slow down or even crash. Therefore, it’s important to monitor the usage of swap memory and try to keep it to a minimum.
In this post we can see How to Extend SWAP Memory
Step 1: check the current swap space by using below command.
[root@linux ~]# free -mt
total used free shared buff/cache available
Mem: 1819 1004 110 31 704 600
Swap: 2047 0 2047
Total: 3867 1004 2157
[root@linux ~]#
Step 2: Make sure, there is enough space in Volume group, if not available kindly get it added from VM/storage. To check the volume group free space, run below command.
[root@linux ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 2 4 0 wz--n- 47.99g <20.00g
Step 3: assume you have space in Volume group, at this stage please turn off the swap memory and check whether swap is 0 or not(swap should be zero after swapoff)
[root@linux ~]# swapoff -av
swapoff /dev/dm-1
[root@linux ~]# free -mt
total used free shared buff/cache available
Mem: 1819 1085 62 31 671 523
Swap: 0 0 0
Total: 1819 1085 62
[root@linux ~]#
Step 4: After turning off the swap memory now extend the logical volume of swap, in this example we are adding additional 2GB to swap memory.
[root@linux ~]# lvextend -L +2G /dev/mapper/rhel-swap
Size of logical volume rhel/swap changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
Logical volume rhel/swap successfully resized.
[root@linux ~]#
Step 5: Now run make swap command to reflect the additional added space.
[root@linux ~]# mkswap /dev/mapper/rhel-swap
mkswap: /dev/mapper/rhel-swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=c31d6231-b665-4bfa-97f4-4b10dec4497d
[root@linux ~]#
Step 6: Turn on the swap memory by using below command.
[root@linux ~]# swapon -av
swapon /dev/mapper/rhel-swap
swapon: /dev/mapper/rhel-swap: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/rhel-swap: pagesize=4096, swapsize=4294967296, devsize=4294967296
[root@linux ~]#
Step 7: Now verify the swap size using below command.
[root@linux ~]# free -mt
total used free shared buff/cache available
Mem: 1819 888 120 32 810 735
Swap: 4095 0 4095
Total: 5915 888 4216
[root@linux ~]#