SWAP: swap is a virtual memory that can be used by server if main memory is short to process the running applications, however swap is very slow in comparison to main memory as the memory calls happens from disk space but at least it provides radial availability of lacking memory to keep processing the data for running applications. Just to add here that main memory is 100 times faster then the SWAP memory
.
Steps below to setup the SWAP space:
Lets just check the Disk space before setting the SWAP space:
[root@techsserver ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda2 xfs 22G 5.1G 17G 24% /
devtmpfs devtmpfs 497M 0 497M 0% /dev
tmpfs tmpfs 520M 0 520M 0% /dev/shm
tmpfs tmpfs 520M 66M 455M 13% /run
tmpfs tmpfs 520M 0 520M 0% /sys/fs/cgroup
tmpfs tmpfs 104M 0 104M 0% /run/user/1000
Also verify the Memory status space before setting up SWAP:
[root@techsserver ~]# free -m
total used free shared buff/cache available
Mem: 991 579 145 62 266 166
Swap: 0 0 0
Now create a file system with space you want to allocate to the SWAP area from your exisiting disks or you can add additioanl disk as well.
Following command will create a file system with the name techsswap under /var with the size of 1GB
sudo /bin/dd if=/dev/zero of=/var/techsswap bs=1M count=1024
Now as we have setup the SWAP, we can notice that 1 GB space is taken out of main Disk space after setting swap space:
[root@techsserver ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda2 xfs 22G 5.1G 17G 24% /
devtmpfs devtmpfs 497M 0 497M 0% /dev
tmpfs tmpfs 520M 0 520M 0% /dev/shm
tmpfs tmpfs 520M 66M 455M 13% /run
tmpfs tmpfs 520M 0 520M 0% /sys/fs/cgroup
tmpfs tmpfs 104M 0 104M 0% /run/user/1000
Next step is to use the newly created file system as a SWAP space by using below command
sudo /sbin/mkswap /var/techsswap
Now apply appropriate permissions to the file system
sudo chmod 600 /var/techsswap
Now lets enable the SWAP space on our server
sudo /sbin/swapon /var/techsswap
As SWAP space is enbaled, we can validate the Memory Status after setting SWAP:
[root@techsserver ~]# free -m
total used free shared buff/cache available
Mem: 991 575 67 62 347 193
Swap: 1023 0 1023
Do not forget to Enable the sawp space on reboot by adding folliwing lines:
swap /var/techsswap swap defaults 0 0