When a Linux system has an out-of-memory error or runs out of RAM, .i.e. can no longer store data in the virtual memory, it will temporarily use swap space in order to increase the size of the virtual memory available. A swap file or one or multiple dedicated swap partitions can be used by the Linux system on a logical volume or a regular filesystem. Resorting to swap space can extend the amount of data that Linux system can keep on its available memory, despite the fact that it uses swap for infrequently used data. This has some drawbacks however. Read/Write speed will be slower when compared to RAM data access which will affect the overall performance considerably.
In a nutshell, mainly for systems with spinning disks, swap space can be a good alternative when the system runs low on RAM .
In this tutorial, you will learn how to add swap space to an Ubuntu 18.04 machine.
Before we start, you should check if your Ubuntu system has swap space enabled already. Type in the following command to find out if your system has a configured swap:
sudo swapon –show
If you no data is returned, your system, at that moment, does not have any swap space available. Otherwise, if your output looks somehow similar to the snapshot above, you already have swap enabled.
You can also check if swap is active by running the command :
If the swap row highlighted above contains positive values, your swap is already active. To find out more about the free command and similar tools, you may want to refer to this article. Sometimes several swap partitions or swap files can be used, but in general your system can use one file.
In the past it was recommended that the swap space should be at least equal to the amount of RAM and should not exceed, as a rule of thumb, twice the amount of RAM depending on the amount of hard disk memory available .
So if a computer’s RAM size is 128KB, a recommended swap partition would be 256KB. Since RAM sizes were quite small in the past, this was the rule. Enabling more than twice the amount of RAM for swap, had no positive effects on the overall performance. Allocating more than 2x RAM for swap space,will lead to thrashing ( happens when both RAM and swap space sizes become almost full . In this case paging blocks of memory between RAM and swap space and back will take a lot of time at the expense of performance. The system will become completely slow if not unresponsive ).
Nowadays the rule above is nonsense to be honest since on modern systems, twice the RAM is quite a lot of space and people would rather prefer to not enable swap altogether.
The table below shows approximately the swap partition recommended sizes by Fedora. This depends on the amount of RAM your system has and on whether you want to have enough memory for hibernation.
Centos and Redhat for instance recommend the following :
Ubuntu however recommends the following :
If RAM < 1 GB, swap >= RAM but < 2x RAM.
If RAM is > 1 GB, swap >= the square root of the RAM but < 2x RAM.
If hibernation is involved (see next section), swap size = RAM + the square root of RAM.
Note that is no definite answer as to what is the correct swap size to use. Even different Linux distros do not offer the same recommendations.
Oftentimes you have the feeling that your Linux system response is slow. Perhaps large applications such as a video editor or LibeOffice or a game are up and running which makes your memory management unit in need of extra memory to cope with the load. A high number of pages used by these memory hungry programs at startup for instance are never used during a given session.The kernel concludes therefore that swapping out those pages will free up the memory for other frontend applications. In such circumstances, swap space will be used to help the kernel manage additional load.
When your computer hibernates, this will entail moving out the physical memory (RAM) content to the swap space before the machine turns off. In this situation the swap partition would need to be at least as big as the main memory (see recommendations above). In Ubuntu, the memory management unit, with regards to hibernation , can not use a swap file on an active file system since it requires a swap/suspend partition.
You may want to read: How to fix high memory usage in Linux
In general swap refers to a partition. This can be seen when, at the time of installation, the user is prompted to configure a swap partition. Any file actually can serve as a swapping device (be it a conventional file or a partition). Swap space can be added by creating a swap file or by increasing the amount of the swap partition.
To check that you have enough space on the disk before creating a swap file. Run the command below for this purpose :
The information above indicates that 1.6G of space is available. This of course should be different in your case.
Provided the amount of available hard disk space above, we can proceed to the creation of our swap file which we will call myswapfile (with 1G of size). It will be located under root / directory . We will be using the fallocate program which will create a file with the desired size. Run the command below :
sudo fallocate -l 1G /myswapfile
Note : suffixes for fallocate size: g = Giga, m = Mega, etc. (Refer to man fallocate)
If fallocate is not installed on your system or you obtain an error message :
‘fallocate failed: Operation not supported’, then you can use the command below to create the swap file:
sudo dd if=/dev/zero of=/myswapfile bs=1024 count=1048576
Next we need to set the permissions on the swap file to 600 in order not to allow other users to read important information from the swap file.
sudo chmod 600 /mnt/myswapfile
Now format the file myswapfile as swap using the mkswap utility:
sudo mkswap /myswapfile
To enable the swap file, issue the following command:
sudo swapon /myswapfile
The newly added swap file is now available and can be checked with: cat /proc/swaps
The steps above will enable the swap file for the currently active session but it will not be permanent.If we reboot however, the system will not keep the swap settings. This can be changed by adding the newly created swap file details to /etc/fstab file. Run the command :
echo ‘/myswapfile swap swap defaults 0 0’ | sudo tee -a /etc/fstab
The way Linux system swaps data out of physical memory to the swap space is configured by the swappiness parameter whose value is between 0 and 100. Small values , i.e. close to zero, instructs the kernel to not swap data out of physical memory for as long as possible. Values close to 100 will lead the kernel to aggressively swap more data out of RAM and move them into swap.
In Ubuntu, the default setting is swappiness=60. For an Ubuntu desktop,reducing this value will improve the overall performance. A value of 10 is recommended, but it is up to you to tune it.
To see the current value of swappiness run the command:
In order for example, to set the swappiness value to 10, issue the command below:
sudo sysctl vm.swappiness=10
Note: For Ubuntu servers, a value of 60 would be more appropriate since they have different performance requirements.
You may want to read: How to speed up Linux
This change of swappiness will not be permanent, i.e. it will remain so until the next reboot. In order to make this change permanent, edit the configuration file /etc/sysctl.conf with your favorite editor:
sudo nano /etc/sysctl.conf
Increase swap space linux
Look up ‘vm.swappiness’ and modify its value as needed. If ‘vm.swappiness’ does not exist however, you can add it at the end of the file as shown below:
Once done, save and reboot.
In order to disable and delete the swap file issue the commands below:
sudo swapoff /myswapfile
sudo rm /myswapfile
Next the swap file would need to be removed from fstab by running the command:
Once the file is in edit mode, removing the swap file related line below :
/myswapfile swap swap defaults 0 0
Swap is used to store temporarily inactive pages. When Linux decides that it needs additional physical memory for active applications in case the amount of RAM is not enough , it will resort to swap in order to free up some memory. This will entail moving out inactive pages to swap so that current processes can benefit from the freed physical memory.
How much swap space is needed will depend on specific requirements since each configuration and setup is unique even between different Linux distros. There are no definite rules though but some rule-of-thumb recommendations.
Users should realize that swap space is not a replacement for RAM. Increasing swap might affect overall performance except in some special situations since this will entail high read/write access times. Not having swap at all might lead to crashes and failure in allocating memory for processes in need of more memory pages.
Swappiness tells Linux how to handle swapping data out of RAM to the swap space. Smaller values means to not swap data out of physical memory for as long as possible. Larger values means more data will be swapped out of RAM and moved into swap.
For a typical Ubuntu desktop, reducing swappiness will improve the performance (a value of 10 is recommended) whereas for Ubuntu servers, the default value of 60 is more reasonable since they have different performance requirements.
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.