RAM is very important for the computer normal operation. It contains all data of the working programs. When the memory is full, the program may produce various errors or even stop working. The response time of the system increases so much that it becomes impossible to work with it.
The easiest way to increase the amount of RAM and protect the system from memory overflow is to add a swap partition. This article will show how to configure swap on Ubuntu 16.04. We will consider the option of adding a swap partition for regular computers, as well as a swap file for servers.
The content of the article
Swap partition is on the HDD, using this method for SSDs can cause problems. As SSD has a limited number of overwrites of one sector then it will reduce the life of your disk. Therefore, the use of swap space on the SSD is strictly not recommended.
What is Swap?
Swap is an area on the hard disk that is used to temporarily store data of main memory if there is no more free space. This allows you to increase the amount of information that the system can store in working memory. The swap space is used only when the memory is already fully occupied, and only data that has been used for a long time will be placed there.
Working with data stored in swap will be much slower than in RAM, but the operating system will continue to work and use swap only for old data. In general, this can be an excellent protection against memory overflow. The paging space can be configured in two ways by adding a ubuntu swap partition to disk or creating a paging file.
Before staring configuration, let’s see if other paging partitions are exist in the system. To do this, run:
$ sudo swapon --show
If nothing is displayed, then swap is not configured on your system. You can also make sure that there is no active swap using the free utility:
$ free -h
total used free shared buff/cache available
Mem: 3.8Gi 1.6Gi 154Mi 3.0Mi 2.1Gi 2.0Gi
Swap: 0B 0B 0B
Swap line is empty, so the swap partition is not yet connected.
Setting up the paging file
Servers often use a paging file, this method is used because it is not always possible to change the disk partition layout.
Check disk space
First let’s make sure that the disk has enough free space. The easiest way to find the free space in the terminal is the df command:
$ sudo df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 393M 1.6M 391M 1% /run
/dev/sda1 20G 9.1G 9.6G 49% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/loop0 4.2M 4.2M 0 100% /snap/gnome-calculator/406
/dev/loop1 90M 90M 0 100% /snap/core/6673
/dev/loop2 152M 152M 0 100% /snap/gnome-3-28-1804/36
/dev/loop3 54M 54M 0 100% /snap/core18/941
/dev/loop4 1.0M 1.0M 0 100% /snap/gnome-logs/57
/dev/loop5 43M 43M 0 100% /snap/gtk-common-themes/701
/dev/loop6 141M 141M 0 100% /snap/gnome-3-26-1604/70
/dev/loop8 54M 54M 0 100% /snap/core18/782
/dev/loop7 141M 141M 0 100% /snap/gnome-3-26-1604/82
/dev/loop9 3.8M 3.8M 0 100% /snap/gnome-system-monitor/70
/dev/loop10 92M 92M 0 100% /snap/core/6531
/dev/loop11 141M 141M 0 100% /snap/gnome-3-26-1604/74
/dev/loop12 15M 15M 0 100% /snap/gnome-characters/254
/dev/loop13 1.0M 1.0M 0 100% /snap/gnome-logs/61
/dev/loop16 15M 15M 0 100% /snap/gnome-characters/206
/dev/loop17 4.2M 4.2M 0 100% /snap/gnome-calculator/352
/dev/loop18 3.8M 3.8M 0 100% /snap/gnome-system-monitor/77
/dev/loop21 152M 152M 0 100% /snap/gnome-3-28-1804/31
/dev/loop22 15M 15M 0 100% /snap/gnome-logs/45
/dev/loop23 2.3M 2.3M 0 100% /snap/gnome-calculator/260
/dev/loop24 36M 36M 0 100% /snap/gtk-common-themes/1198
tmpfs 393M 16K 393M 1% /run/user/123
tmpfs 393M 0 393M 0% /run/user/1000
/dev/loop25 54M 54M 0 100% /snap/core18/970
/dev/loop26 90M 90M 0 100% /snap/core/6818
/dev/loop15 15M 15M 0 100% /snap/gnome-characters/258
/dev/loop27 3.8M 3.8M 0 100% /snap/gnome-system-monitor/81
/dev/loop28 152M 152M 0 100% /snap/gnome-3-28-1804/40
In our case there is enough space on/dev/sda1 partition to accommodate paging space. The size of the swap Ubuntu 16.04 depends on your needs. Usually it is recommended to use the amount of two times the actual size of the RAM. But more than 4 GB is not needed. If you plan to use hibernation on a personal computer, then you need to use a volume equal to the amount of RAM.
Creating a paging file
The fastest way to create a swap file ubuntu is such a file, it is to use the fallocate utility, it creates the file of the desired size instantly. Let’s assume that we have 1 GB of RAM, we’ll add a 2 GB ubuntu swap partition. To do this, run:
sudo fallocate -l 1G /swapfile
See if the right amount of memory is really reserved:
$ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G May 22 10:56 /swapfile
When the file is ready, we need to turn it into a Ubuntu swap file. First you need to block access to it by everyone except the superuser. To do this, run:
sudo chmod 600 /swapfile
Now only root can read and modify this file:
ls -lh / swapfile
Then create the swap file system with the command:
sudo mkswap /swapfile
You can make sure that everything works with the command:
sudo swapon --show
One more time let’s check disk free space:
As you can see, the paging file was successfully created and activated. Now the memory is much more and the system will not freeze. But it will only work until the reboot.
Permanent swap file
As we said, this swap will only work until the server or computer is rebooted. To configure the Swap Ubuntu 16.04 to be saved after the reboot, you need to edit the /etc/fstab file. You can manually add a line to the file, but you can use the following command:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Setting up a swap partition in Ubuntu
For configureint swap as a partition all actions are similar to those used for a file, only a section is used instead of a file. Suppose we want to use the /dev/sda2 partition. First create a Ubuntu swap from a regular partition:
sudo mkswap /dev/sda2
Then activate the swap partition:
sudo swapon /dev/sda2
Check if everything went right:
sudo swapon --show
If yes, then you will see the necessary one in the list of connected partitions. It remains to add the line in /etc/fstab for automatic activation after a reboot:
echo '/dev/sda2 none swap sw 0 0' | sudo tee -a /etc/fstab
You can control the amount of swap space usage in Ubuntu using the swappiness kernel parameter:
The default value is 60. If this value is closer to 100, the system will try to put more data into the paging and save memory, with values close to zero, the kernel does not transfer data to disk unless it is absolutely necessary. This option can make the system faster, especially for home computers.
To set the swappiness value to 70 run:
sudo sysctl vm.swappiness=70
vm.swappiness = 70
Another parameter that affects the amount of memory used is vfs_cache_pressure. It tells the system how long it is necessary to keep open files in the RAM cache. A value of 100 causes the system to delete such files very quickly, 0 – to keep them in memory as long as possible. See current status:
To keep files longer, you can set the value to 50:
sudo sysctl -w vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50
This guide will help you temporarily solve the problem with paging space if the server does not have enough memory, or it can help prevent the computer from hanging. Setting up swap Ubuntu is very easy. If you have any questions, ask in the comments!