Tested on Ubuntu Server 18.04, 20.04
Swapis a portion of hard drive storage that has been set aside for the OS to temporarily store data that it can no longer hold in RAM.- 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.
- Though the information written to disk will be significantly slower than information kept in RAM, the OS will prefer to keep running application data in memory and use swap for the older data.
It is believed that placing swap partitions on an SSD drive is not advisable, as it may harm the device. Actually, the write-cycle on modern SSDs is good enough that you'll likely replace the drive before it becomes a problem. Read this Reddit post, these AskUbuntu one and one.
Still concern about SSD thing? How to check if my Ubuntu is placed on SSD or HDD? Read this AskUbuntu answer.
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 -h# Output
total used free shared buff/cache available
Mem: 981Mi 122Mi 647Mi 0.0Ki 211Mi 714Mi
Swap: 0B 0B 0Bdf / -h# Output
Filesystem Size Used Avail Use% Mounted on
/dev/vda2 40G 7.2G 33G 18% /82% unused space is generous for swap. Your server's mileage may vary.
Rules of thumb:
- An amount equal to or double the amount of RAM on your system is a good size of a swap space.
- Anything over 4G of swap is probably unnecessary if you are just using it as a RAM fallback.
We will allocate a file of the size that we want called swapfile in our root (/) directory.
The best way of creating a swap file is with the fallocate program. This command instantly creates a file of the specified size.
Since the server in our example has 1G of RAM, we will create a 1G file in this guide. Adjust this to meet the needs of your own server:
sudo fallocate -l 1G /swapfile# Make the file only accessible to `root`
sudo chmod 600 /swapfile
# Mark the file as swap space
sudo mkswap /swapfileThen enable the swap file, allowing our system to start using it.
sudo swapon /swapfileImportant
You might encounter error:
swapon: /swapfile: swapon failed: Invalid argument
# In kernel log `/var/log/kern.log`
swapon: swapfile has holesUse this command in step 3 instead:
dd if=/dev/zero of=/swapfile bs=1M count=1024Verify that the swap is available:
sudo swapon --show
# Output
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -2# Back up the `/etc/fstab` file in case anything goes wrong
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabNote
Remember, interactions with the swap file are “expensive” in that they take a lot longer than interactions with RAM and they can cause a significant reduction in performance. Telling the system not to rely on the swap much will generally make your system faster.
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.
View the current swappiness value:
cat /proc/sys/vm/swappiness
# Output
60For a server, you might want to move it closer to 0.
sudo sysctl vm.swappiness=10This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our /etc/sysctl.conf file:
sudo nano /etc/sysctl.conf
# bottom of `/etc/sysctl.conf`
vm.swappiness=10vfs_cache_pressure configures how much the system will choose to cache inode and dentry information over other data. Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it’s an excellent thing for your system to cache.
cat /proc/sys/vm/vfs_cache_pressure
# Output
100As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50:
sudo sysctl vm.vfs_cache_pressure=50Again, this is only valid for our current session. We can change that by adding it to our configuration file like we did with our swappiness setting:
sudo nano /etc/sysctl.conf
# bottom of `sudo nano /etc/sysctl.conf`
vm.vfs_cache_pressure=50