Saturday, 9 November 2019

How to Increase Swap Memory in CentOS 7 (Using Swap File)



Linux distributions has the facility to reserve some amount of disk space to use as virtual memory when the amount of physical memory (RAM) is full. Swapping is a method when the RAM is fully utilized, inactive pages in the memory are moved to the swap space from the RAM to swap space.
This article describes How to increase swap memory in CentOS 7 when the Linux machine is running out of swap space.

Step 1. Swap Space Check: Run the following command to check existing swap space:

[root@linuxcnf ~]# swapon -sh
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1048572 0       -2
[root@linuxcnf ~]#

[root@linuxcnf ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1838         140        1528           8         169        1549
Swap:          1023           0        1023
[root@linuxcnf ~]#

In the above output the existing swap size is around 1 GB. We will extend swap space from 1 GB to 2 GB.

Step 2. Swap File: Run the dd command to create a swap file of 1 GB:

[root@linuxcnf ~]# dd if=/dev/zero of=/var/swapfile bs=1G count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 6.90533 s, 155 MB/s
[root@linuxcnf ~]#

[root@linuxcnf ~]# chmod 600 /var/swapfile
[root@linuxcnf ~]#

[root@linuxcnf ~]# ls -lh /var/swapfile
-rw-------. 1 root root 1.0G Nov  3 19:12 /var/swapfile
[root@linuxcnf ~]#

Step 3. Enabling Swap Space: Run the following command to make the file as swap file:

[root@linuxcnf ~]# mkswap /var/swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=b0cecca1-4d2b-47b8-8bf1-af8e74ea39c8
[root@linuxcnf ~]#

Enable the swap file as swap space:

[root@linuxcnf ~]# swapon /var/swapfile
[root@linuxcnf ~]#

Add the below entry in /etc/fstab configuration file to auto mount swap space on reboot:

[root@linuxcnf ~]# vi /etc/fstab
………….
/var/swapfile           swap                    swap    defaults        0 0
[root@linuxcnf ~]#

Save and exit from the file.

Step 4. Verify the Swap Space: Swap configuration is done now run the following commands to verify swap space:

[root@linuxcnf ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1048572 0       -2
/var/swapfile                           file    1048572 0       -3
[root@linuxcnf ~]#

[root@linuxcnf ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1838         141        1081           8         614        1541
Swap:          2047           0        2047
[root@linuxcnf ~]#

Done!!! Now Swap Space extended from 1 GB to 2 GB.

No comments:

Post a comment