Increase swap memory in centos 7
When operating system no longer can put data on RAM then it uses some area of hard disk space to store data that area is called swap space. When the system goes on low memory mode then it uses swap space to store data.
Swap space/partition is space on a disk created for use by the operating system when memory has been fully utilized. It can be used as virtual memory for the system.it can either be a partition or a file on a disk.
When the kernel runs out of memory, it can move idle/inactive processes into swap creating room for active processes in the working memory.
There are two methods to increase the swap memory after installation.
First method as Fdisk utility
To check the system for swap information, use swapon -s option.
[root@centos ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 1535996 0 -1
To check system total memory type below command
[root@centos ~]# free -m
total used free shared buff/cache available
Mem: 2711 299 2009 9 402 2226
Swap: 1499 0 1499
Before allocating swap, check the space availability of drive using the following command.
[root@centos ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 22G 4.5G 16G 23% /
devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs 1.4G 0 1.4G 0% /dev/shm
tmpfs 1.4G 9.2M 1.4G 1% /run
tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/sda1 976M 134M 776M 15% /boot
tmpfs 272M 12K 272M 1% /run/user/42
tmpfs 272M 0 272M 0% /run/user/0
Next we will create swap space partition
[root@centos ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e):
Using default response e
Selected partition 4
First sector (50227200-60494175, default 50227200): --- press Enter
Using default value 50227200
Last sector, +sectors or +size{K,M,G} (50227200-60494175, default 60494175):--press Enter
Using default value 60494175
Partition 4 of type Extended and of size 4.9 GiB is set
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (50229248-60494175, default 50229248): --- press Enter
Using default value 50229248
Last sector, +sectors or +size{K,M,G} (50229248-60494175, default 60494175): +1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1-5, default 5): 5
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): p
Disk /dev/sda: 31.0 GB, 30973018112 bytes, 60494176 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000e9b95
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 5171199 1536000 82 Linux swap / Solaris
/dev/sda3 5171200 50227199 22528000 83 Linux
/dev/sda4 50227200 60494175 5133488 5 Extended
/dev/sda5 50229248 52326399 1048576 82 Linux swap / Solaris
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
Refresh the system settings use command partprobe.
[root@centos ~]# partprobe /dev/sda
you can format the swap partition by using mkswap
[root@centos ~]# mkswap /dev/sda5
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=db3f0cab-e45c-4c38-b8bf-7901833dd542
Activate swap partition using below command
[root@centos ~]# swapon /dev/sda5
use command to verify the activated partition
[root@centos ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda2 partition 1535996 0 -1
/dev/sda5 partition 1048572 0 -2
Add the newly created partition to fstab configuration file
[root@centos ~]# vi /etc/fstab
Add the following entry at the end of the file.
/dev/sda5 swap swap defaults 0 0
save and exit fstab file and Restart the system by reboot command.
[root@centos ~]# reboot
After reboot, check the created partition to be listed or not
[root@centos ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda5 partition 1048572 0 -1
/dev/sda2 partition 1535996 0 -2
Second Method usig File.
No additional hard drive available in system, create a file for use the swap space increase.
you can use following command to create a swap file under /root directory.
[root@centos ~]# dd if=/dev/zero of=/root/swapfile bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 10.6921 s, 100 MB/s
Alternatively, you can use the fallowcate command as follows.
fallocate -l 1G /swapfile
[root@centos ~]# ls -l /root/swapfile
-rw-r--r-- 1 root root 1073741824 Aug 24 17:14 /root/swapfile
Now change permissions of swap file.
[root@centos ~]# chmod 600 /root/swapfile
[root@centos ~]# ls -l /root/swapfile
-rw------- 1 root root 1073741824 Aug 24 17:14 swapfile
Make the file as swap file using below command.
[root@centos ~]# mkswap /root/swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=7ccb2bc6-f5fd-4c4b-9b1f-8b571284e026
Activate the newly created swap file using swapon
[root@centos ~]# swapon /root/swapfile
Now open /etc/fstab file and /root/swapfile at the end of the file.
[root@centos ~]# vi /etc/fstab
/root/swapfile swap swap defaults 0 0
save and exit.
Verify whether the newly created swap area is available or not.
[root@centos ~]# swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 1048572 0 -1
/dev/sda2 partition 1535996 0 -2
/root/swapfile file 1048572 0 -3
Finally check free memory.
[root@centos ~]# free -m
total used free shared buff/cache available
Mem: 2711 300 971 9 1439 2223
Swap: 3547 0 3547
Thats it!!! Now swap is fine.
Changing swappiness parameter
The swappiness parameter is used to configure how often the swap space should be used. The swappiness parameter value lies between 0 to 100. As the value is set to smaller the system will avoid using swap space and if the value is bigger then the system will use swap space much often. The default swappiness value on the CentOS system is 30. The value 30 is ok for desktop machine while you should change it for the production system.
[root@centos ~]# cat /proc/sys/vm/swappiness
30
To set the swappiness value to 10, run below command
[root@centos ~]# sysctl vm.swappiness=10
vm.swappiness = 10
[root@centos ~]# cat /proc/sys/vm/swappiness
10
To make the changes permanent edit /etc/sysctl.conf file and add as below
[root@centos ~]# vi /etc/sysctl.conf
vm.swappiness=10
Deactivate swap file
To deactivate swap space run following command
[root@centos ~]# swapoff -v /root/swapfile
swapoff /root/swapfile
Now edit /etc/fstab file remove /root/swapfile entry line from it and save.
and remove /root/swapfile from system with rm command
[root@centos ~]# rm /root/swapfile
you have successfully learned how to increase/add Swap Space on CentOS 7