Configure Swap Space for Linux Server
Most of the content comes from this article.
Scenario
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.
1 | free -h |
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.
1. Configure Swap Space
1.1 Checking Configuration
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.
1 | 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.
1 | df -h |
The device with /
in the Mounted on
column is our disk in this case, and we still have plenty of space (21G) available.
1.2 Creating a Swap File
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.
1 | 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
command.
1 | ls -lh /swapfile |
If you care about accessibility, you can make it only accessible by root.
1 | 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 mkswap
command.
1 | 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.
1 | sudo swapon /swapfile |
1.3 Marking the Swap File Permanent
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 /etc/fstab
file.
It is recommended to back up /etc/fstab
in case anything goes wrong.
1 | 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.
1 | echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab |
1.4 Tuning Swap Settings
There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap.
1.4.1 Swappiness
The 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.
1 | cat /proc/sys/vm/swappiness |
For server, this value might better be close to zero. We can change this value by this command.
1 | 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.
1 | sudo vim /etc/sysctl.conf |
Then, add this line at the bottom, or change the value is entry already exists.
1 | vm.swappiness=10 |
1.4.2 Cache Pressure
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 inode
and dentry
information over other data. And by default it is 100, which removes cache too quickly.
1 | cat /proc/sys/vm/vfs_cache_pressure |
We can set this to a more conservative setting like 50, and the method is similar to how we configure swapiness.
1 | sudo sysctl vm.vfs_cache_pressure=50 |
Also, we can make it permanent by adding an extra entry in /etc/sysctl.conf
.
1 | vm.vfs_cache_pressure=50 |
Epilogue
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! 🥳