Swapfiles are important because they prevent a memory constrained device from crashing due to load. From https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04
One way to guard against out-of-memory errors in applications is to add some swap space to your server. In this guide, we will cover how to add a swap file to an Ubuntu 22.04 server.
Swap is a portion of hard drive storage that has been set aside for the operating system to temporarily store data that it can no longer hold in RAM. This lets you increase the amount of information that your server can keep in its working memory, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.
The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fallback for when your system’s RAM is depleted can be a good safety net against out-of-memory exceptions on systems with non-SSD storage available.
Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough.
We can see if the system has any configured swap by typing:
sudo swapon --showIf you don’t get back any output, this means your system does not have swap space available currently.
You can verify that there is no active swap using the free utility:
free -hOutput:
total used free shared buff/cache available
Mem: 981Mi 122Mi 647Mi 0.0Ki 211Mi 714Mi
Swap: 0B 0B 0B
As you can see in the Swap row of the output, no swap is active on the system.
Check your current disk usage:
df -hOutput:
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 25G 1.4G 23G 7% /
Although there are many opinions about the appropriate size of a swap space, it really depends on your personal preferences and your application requirements. Generally, an amount equal to or double the amount of RAM on your system is a good starting point.
Create a 1G swap file:
sudo fallocate -l 1G /swapfileVerify:
ls -lh /swapfileExpected Output:
-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile
Secure the swap file:
sudo chmod 600 /swapfile
ls -lh /swapfileOutput:
-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile
Mark and enable the file:
sudo mkswap /swapfile
sudo swapon /swapfileCheck it’s working:
sudo swapon --show
free -hBackup and modify fstab:
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabcat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
sudo nano /etc/sysctl.confAdd:
vm.swappiness=10
cat /proc/sys/vm/vfs_cache_pressure
sudo sysctl vm.vfs_cache_pressure=50
sudo nano /etc/sysctl.confAdd:
vm.vfs_cache_pressure=50
Following the steps in this guide will give you some breathing room in cases that would otherwise lead to out-of-memory exceptions. Swap space can be incredibly useful in avoiding some of these common problems.
If you are running into OOM (out of memory) errors, or if you find that your system is unable to use the applications you need, the best solution is to optimize your application configurations or upgrade your server.