You must create swap space in Linux for better performance. Every server needs swap space, even if it’s never going to use it. Swap space is allocated when your server is completely out of memory, and using swap space allows your server to continue to offer its services. Therefore, you should always have at least a minimal amount of swap space available.
You normally create swap space while installing the server, but you can also add it later. To create swap space is a four step procedure.
Step 1: Create a device you’re going to use as the swap device using below command:
[root@XXXX~]# dd if=/dev/zero of=/swapfile bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 58.7454 s, 18.3 MB/s
Step 2: Use mkswap to format the newly created swap device. This is same as creation of a file system on a storage device.
[root@XXXXX ~]# mkswap /swapfile Setting up swapspace version 1, size = 1048572 KiB no label, UUID=d9e09cbe-9189-409c-8fa1-cf0f99d17722
Type free -m to verify the current amount of swap space on your server. This amount is given in megabytes.
[root@XXXX ~]# free -m total used free shared buff/cache available Mem: 991 71 65 12 854 752 Swap: 0 0 0
Step 3: You must use swapon command for activating the swap space. You can compare this to the mounting of the file system, which ensures you can actually put files on it.
[root@XXXX ~]# swapon /swapfile
Type free -m again to verify that you just added 1GB of swap space.
[root@XXXX ~]# free -m total used free shared buff/cache available Mem: 991 73 62 12 855 749 Swap: 1023 0 1023
Step 4: The above method is not persistent across reboot meaning that its not mount this space after reboot of the server to do you must Create a line in /etc/fstab to activate the swap space automatically the next time you reboot your server.
/swapfile swap swap defaults 0 0