How to Enable Or Add Swap Space on Ubuntu 16.04

In this article, we will learn how to enable or add the swap partition on Ubuntu 16.04, actually, the swap will increase the chance of responsiveness of the servers by guarding the out of memory errors for the applications, by default Ubuntu will not enable the swap space. We will learn how to add or enable the swap file for Ubuntu 16.04 servers.
Swap is the location or the file which can handle the temporary data which is not required immediately by RAM but will be used to increase the amount of information that the server can handle for its working process, and it will be used when there is no RAM available for the application to hold.
Before we begin, we will first check for the swap space available on the server or system
We can use the below command to see that the system is having the swap partition or not
$ free -h
total    used    free    shared    buffers    cached
Mem:      2.0G    1.9G    80M       11M       156M       1.0G
-/+ buffers/cache: 738M    1.2G
Swap:      0B       0B      0B
We can also run the below command but if the swap partitions do not exist, we cannot see any information.
$ sudo swapon -s
Filename Type Size Used Priority
In the above command, we can see that the swap is not enabled or not configured for this server to configure the swap in this machine. We will first check for the free disk space available with the below command –
$ df –h
Filesystem    Size    Used    Avail    Use%    Mounted on
udev          996M    4.0K    996M       1%     /dev
tmpfs         201M    396K    200M       1%     /run
/dev/xvda1    7.8G    5.8G    1.7G       79%    /
none          4.0K       0    4.0K       0%     /sys/fs/cgroup
none          5.0M       0    5.0M       0%     /run/lock
none          1001M      0    1001M      0%     /run/shm
none          100M       0    100M       0%     /run/user
We can see some space on the /dev/xvda1 or we can attaché the new disk drive to use for the swap partitions. In this case, we will use the /dev/xvda1 for the swap partition.
As we know the disk space availability, we can go ahead and create a swap file on the filesystem. To create the swap file we can use ‘fallocate’ a package or utility which can create a preallocated size to instantly. As we have a little space on the server will create a swap file with 512 MB size to create a swap file below is the command.
$ sudo fallocate -l 512M /swapfile
And to check the swap file we will use the below command
$ ls -lh /swapfile
-rw-r--r-- 1 root root 512M Sep 6 14:22 /swapfile
Before, we are going to enable the swap, we need to fix the file permission that other than root any others can read/write the file below is the command to change the file permission.
$ sudo chmod 600 /swapfile
Once, we change the permission we will check the file below and execute the below command to check the swap file permissions.
$ ls -lh /swapfile
-rw------- 1 root root 512M Sep 6 14:22 /swapfile
We will now make this file as a swap space using this below command –
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=d02e2bbb-5fcc-4c7b-9f85-4ae75c9c55f9
Now we will enable the swap by using the below command
$ sudo swapon –s
Filename Type Size Used Priority
/swapfile file 524284 0 -1
We can also check with free –h commands to see the swap partition
$ free –h
total    used    free    shared    buffers    cached
Mem:    2.0G       1.8G    110M       11M       189M    940M
-/+ buffers/cache: 759M 1.2G
Swap:    511M       0B    511M
As in the above steps, we have created the swap partition and we are able to use that swap for temporary memory and once the machine is rebooted then the swap, setting will be lost to needed to use this swap file permanently we will make the swap file permanent.
We will edit the /etc/stab and add the information to mount the swap file even if we reboot the machine
$ sudo vi /etc/fstab
Add the below line to the existing file.
/swapfile none swap sw 0 0
For better performance for using the swap memory, we can do some tweaks.
We can see that, the current Cache pressure setting by using the below command
$ cat /proc/sys/vm/vfs_cache_pressure
100
The present settings will remove the cache information more quickly, we will set these settings to 60 by using the below command
$ sudo sysctl vm.vfs_cache_pressure=60
vm.vfs_cache_pressure = 60
As this setting is temporary once the system is rebooted all this setting will be lost so to make it permanent edit the /etc/sysctl.conf file and add the following line ‘vm.vfs_cache_pressure=60’
$ sudo vi /etc/sysctl.conf
# Accept ICMP redirects only for gateways listed in our default
# gateway list (enabled by default)
# net.ipv4.conf.all.secure_redirects = 1
#
# Do not send ICMP redirects (we are not a router)
#net.ipv4.conf.all.send_redirects = 0
#
# Do not accept IP source route packets (we are not a router)
#net.ipv4.conf.all.accept_source_route = 0
#net.ipv6.conf.all.accept_source_route = 0
#
# Log Martian Packets
#net.ipv4.conf.all.log_martians = 1
#
vm.vfs_cache_pressure=60
The swappines is the configuration settings where it defines, how often the swap data is to be shared or moved to the swap space. The value of the swappiness is 0 to 100 percentage and value near to the zero will define the swappiness to swap data unless it needed to move, and a value close to 100 will put the date into the swap more frequently to keep the RAM more free.
We can see the current setting by using the below command
$ cat /proc/sys/vm/swappiness
60
If its desktop we can use this swappines setting, but we are configuring the servers the swappiness should be near to 10-20.
Here we are configuring the swappiness to 20 and below is the command to set the swappiness to 20
$ sudo sysctl vm.swappiness=20
vm.swappiness = 20
And the setting is temporary and if we needed to add this swappiness setting permanently we needed to edit the /etc/sysctl.conf and add the following lines to end of the file
$ sudo vi /etc/sysctl.conf
# Accept ICMP redirects only for gateways listed in our default
# gateway list (enabled by default)
# net.ipv4.conf.all.secure_redirects = 1
#
# Do not send ICMP redirects (we are not a router)
#net.ipv4.conf.all.send_redirects = 0
#
# Do not accept IP source route packets (we are not a router)
#net.ipv4.conf.all.accept_source_route = 0
#net.ipv6.conf.all.accept_source_route = 0
#
# Log Martian Packets
#net.ipv4.conf.all.log_martians = 1
#
vm.vfs_cache_pressure=50
vm.swappiness=10
In the above article, setup and configuration we can solve the out-of-memory problems with the server. Where we can create a swap partition and also tune the swap settings for more accurate usage of the swap partitions.