Skip to content

Instantly share code, notes, and snippets.

@bonniss
Last active June 16, 2024 15:07
Show Gist options
  • Save bonniss/7dfaa5d8630599c0f729f272cdb35fc6 to your computer and use it in GitHub Desktop.
Save bonniss/7dfaa5d8630599c0f729f272cdb35fc6 to your computer and use it in GitHub Desktop.
Guide to add Swap Space on Ubuntu

Guide to add Swap Space on Ubuntu

Tested on Ubuntu Server 18.04, 20.04

  • Swap is 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.

Step 1 – Checking the System for Swap Information

sudo swapon --show

If 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          0B

Step 2 – Checking Available Space on the Hard Drive Partition

df / -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.

Step 3 – Creating a Swap File

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

Step 4 – Enabling the Swap File

# Make the file only accessible to `root`
sudo chmod 600 /swapfile

# Mark the file as swap space
sudo mkswap /swapfile

Then enable the swap file, allowing our system to start using it.

sudo swapon /swapfile

Important

You might encounter error:

swapon: /swapfile: swapon failed: Invalid argument

# In kernel log `/var/log/kern.log`
swapon: swapfile has holes

Use this command in step 3 instead:

dd if=/dev/zero of=/swapfile bs=1M count=1024

Verify that the swap is available:

sudo swapon --show

# Output
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

Step 5 – Making the Swap File Permanent

# 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/fstab

Optional - Tuning Swap settings

Note

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.

Lower 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.

View the current swappiness value:

cat /proc/sys/vm/swappiness

# Output
60

For a server, you might want to move it closer to 0.

sudo sysctl vm.swappiness=10

This 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=10

Lower the Cache Pressure Setting

vfs_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
100

As 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=50

Again, 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

Reference

[Digital Ocean] How To Add Swap Space on Ubuntu 20.04.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment