Most of the content comes from this article.
Have you ever experienced out of memory error on a Linux server? Or, have you been bothered by the freeze that
conda install caused? Well, the problem probably due to insufficient memory. But, how? Linux should have a good memory management. 😫 So, let’s have a look at our memory.
Why there is no swap?😵💫 I don’t know the reason, but maybe it is not configured by default. So in this post, I’m going to introduce how to configure swap file for a Linux server.
Before we start, we can have a brief look at our existing configurations. For example, we can see if the system has any configured swap by typing this.
sudo swapon --show
No output means that you don’t yet have any swap space. And you can use
free command to confirm that, like we used just now.
Then, to add a swap file, we may need to check our disk usage to make sure we have enough space.
The device with
/ in the
Mounted on column is our disk in this case, and we still have plenty of space (21G) available.
Now that we know the absence of our swap file, and how much disk space we have, we can start to create one now.
The best way of creating a swap file is with the
fallocate program. This command instantly creates a file of the specified size.
sudo fallocate -l 6G /swapfile
How much should be the swap size? For more information, you can refer to this article.
To ensure that we have created the swap file, we can verify it simply by
ls -lh /swapfile
If you care about accessibility, you can make it only accessible by root.
sudo chmod 600 /swapfile
A little tip here, you can use
-h parameter to show space size in automatic units rather than huge numbers in Byte.
After that, we have to mark it as swap by
sudo mkswap /swapfile
Now, we can enable the swap file. And we can see the properties by
--show parameter, instead of nothing at the very beginning.
sudo swapon /swapfile
Although we enabled swap file, the changes are only for the current session, and will be lost if we reboot. So we can make it permanent by add it to
It is recommended to back up
/etc/fstab in case anything goes wrong.
sudo cp /etc/fstab /etc/fstab.bak
We can add our new swap file information at the end of
/etc/fstab file simply by this.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap.
swappiness parameter configures how often your system swaps data out of RAM to the swap space. This is a value between 0 and 100 that represents a percentage. With values close to zero, the kernel will not swap data to the disk unless absolutely necessary. On the contrary, values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free.
We can see our current swappiness value by this command. You may see “60” as a default value.
For server, this value might better be close to zero. We can change this value by this command.
sudo sysctl vm.swappiness=10
Still, this will persist only during the current session. You can add it to
/etc/sysctl.conf to make it persistent.
sudo vim /etc/sysctl.conf
Then, add this line at the bottom, or change the value is entry already exists.
Another related value that you might want to modify is the
vfs_cache_pressure. This setting configures how much the system will choose to cache
dentry information over other data. And by default it is 100, which removes cache too quickly.
We can set this to a more conservative setting like 50, and the method is similar to how we configure swapiness.
sudo sysctl vm.vfs_cache_pressure=50
Also, we can make it permanent by adding an extra entry in
Now that we have configured swap space for our Linux server, we won’t worry about memory problem any more. 😁
For example, in the case below, used swap space is more than free memory we have, which would likely to cause memory error before. But now, it won’t! 🥳