The simple way of increasing the responsiveness of your server against out of memory errors in server to add some swap space. In this artical, we will help how to create a swap file to an Ubuntu 16.04 &18.04 &19.04 server.
What is Swap?
The Linux Kernel usually uses your RAM memory to store temporary information.When there is no enough RAM space, the Linux Kernel takes some of this information from RAM and writes it to the swap space on a hard drive.
This is called the swapping process. This way your Linux system can release some RAM space.and doesn’t crash due to lack of memory. So, Linux swap is a very useful way to extend the RAM because it provides the necessary
additional memory when the RAM space has been exhausted and a process has to be continued.
Check the System for Swapspace Information
You can check type of your Swapspace with this command:
$ sudo swapon --show
If you are not getting any output that means no swapspace available on your system currently.
You can verify no active swap using the free command
$ free -h
linuxworld.info@virtualBox~$ swapn total used free shared buffers cached total used free shared buffers cached Mem: 1518 1459 59 0 180 631 -/+ buffers/cache: 647 871 Swap: 2047 0 2047
Check Available Space on the Hard Drive Partition
Before start first check the current disk space using this command
$ df -h Filesystem Size Used Avail Use% Mounted on udev 2.0G 0 2.0G 0% /dev tmpfs 395M 776K 394M 1% /run /dev/xvda1 25G 11G 14G 44% / tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/loop1 89M 89M 0 100% /snap/core/7396 tmpfs 395M 0 395M 0% /run/user/1000
/dev/xvda1 is our disk in this case
Create a Swap File
create a swap file, run this command first and replace 2G with the value you want.
$sudo fallocate -l 2G /swapfile
You can verify that correct amount space reserved for swap memory
$ ls -lh /swapfile -rw------- 1 root root 2.0G Aug 26 18:25 /swapfile
Finally, Enable The Swap.
You have to set the correct permissions of the file so that only the
root privileges can read the contents
Set the file permission only accessible to
root user by typing:
sudo chmod 600 /swapfile
Check permissions typing this command:
ls -lh /swapfile -rw------- 1 root root 2.0G Aug 26 18:25 /swapfile
Then mark the file as swap by typing this command:
$ sudo mkswap /swapfile Output Setting up swapspace version 1, size = 2048 MiB (2147483648 bytes) no label, UUID=6e755505-3ab8-450g-aed3-596e7608sdge
You can enable swapfile using typing this command:
$ sudo swapon /swapfile
Verify Swapspace is enable or not typing this command:
$ sudo swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2
You can verify the output use
free utility command :
$ free -h total used free shared buff/cache available Mem: 3.9G 2.7G 382M 2.4M 836M 980M Swap: 2.0G 0B 2.0G
Make the Swap File Permanent
First, Back up the
/etc/fstab file in if any case anything goes wrong:
$ sudo cp /etc/fstab /etc/fstab.bak
If you want the changes to be permanent, you need to edit the /etc/fstab file open it and add the following.
/swapfile none swap sw 0 0
Ctrl+O to save the changes and Ctrl+X to exit
In the end, check the status of the swapfile:
$free -h total used free shared buff/cache available Mem: 3.9G 2.7G 377M 2.4M 837M 976M Swap: 2.0G 0B 2.0G
Define How Often The Swapspace Will Be Used.
So, every time your system need to use Swapspace, you will notice that some processes and applications will run slower. However, you can adjust the system to use much more RAM than swap. This can help improve overall system performance. Normally, the default swappiness value is 60.The smaller this value, the more of your RAM will be used. Swap tendency is defined by this formula.
So, to check your current the swappiness value, run this command:
$ cat /proc/sys/vm/swappiness Output 60
You should see the value of 60.
If you want to modify the default value, you need to edit the file /etc/sysctl.conf.
And add the following:
10 is the most commonly recommended value.
Also, this setting is recommended when you have more than 2Gb of RAM.
Save the file and close it with Ctrl+O and Ctrl+X keys shortcuts. In order to apply the changes, you need to reboot the system. Now, the swappiness values is ten,This way your Linux kernel will use more RAM and less swap.
All Done Enjoy 🙂