DEV Community

David Johnson
David Johnson Subscriber

Posted on

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.

We can see if the system has any configured swap by typing:

sudo swapon --show
Enter fullscreen mode Exit fullscreen mode

If it has one, we're going to want to get rid of it and create a new one. You can do that with the following command:

sudo swapoff -v /swapfile
Enter fullscreen mode Exit fullscreen mode

And then remove the file

sudo rm /swapfile
Enter fullscreen mode Exit fullscreen mode

Now we want to create a new swap file. We'll set the size parameter to 8G as we want to use some VMware in Ubuntu

sudo fallocate -l 8G /swapfile
Enter fullscreen mode Exit fullscreen mode

Make the file only accessible to root by typing:

sudo chmod 600 /swapfile
Enter fullscreen mode Exit fullscreen mode

We can now mark the file as swap space by typing:

sudo mkswap /swapfile
Enter fullscreen mode Exit fullscreen mode

After marking the file, we can enable the swap file, allowing our system to start using it:

sudo swapon /swapfile
Enter fullscreen mode Exit fullscreen mode

Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically. We can change this by adding the swap file to our /etc/fstab file.

Back up the /etc/fstab file in case anything goes wrong:

sudo cp /etc/fstab /etc/fstab.bak
Enter fullscreen mode Exit fullscreen mode

Add the swap file information to the end of your /etc/fstab file by typing:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Enter fullscreen mode Exit fullscreen mode

There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap.

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

Values that are closer to 100 will try to put more data into swap in an effort to keep more RAM space free. Depending on your applications’ memory profile or what you are using your server for, this might be better in some cases.

We can see the current swappiness value by typing:

cat /proc/sys/vm/swappiness
Enter fullscreen mode Exit fullscreen mode

For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might want to move it closer to 0.

We can set the swappiness to a different value by using the sysctl command.

For instance, to set the swappiness to 10, we could type:

sudo sysctl vm.swappiness=10
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

At the bottom, you can add vm.swappiness=10. Save and close the file when you are finished (in nano, press Ctrl+x and it will prompt you to save before exiting).

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.

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. You can see the current value by querying the proc filesystem again:

cat /proc/sys/vm/vfs_cache_pressure
Enter fullscreen mode Exit fullscreen mode

As it is currently configured (100), our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:

sudo sysctl vm.vfs_cache_pressure=50
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

At the bottom, add the line that specifies your new value:

vm.vfs_cache_pressure=50
Enter fullscreen mode Exit fullscreen mode

Save and close the file when you are finished.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay