Skip to content

Instantly share code, notes, and snippets.

@davidisnotnull
Created February 2, 2024 16:33
Show Gist options
  • Save davidisnotnull/7c0314081be09fc0076746b4e36efc71 to your computer and use it in GitHub Desktop.
Save davidisnotnull/7c0314081be09fc0076746b4e36efc71 to your computer and use it in GitHub Desktop.
How To Add Swap Space on Ubuntu 22.04

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

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

And then remove the file

sudo rm /swapfile

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

Make the file only accessible to root by typing:

sudo chmod 600 /swapfile

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

sudo mkswap /swapfile

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

sudo swapon /swapfile

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

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

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

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

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

At the bottom, you can add vm.swappiness=10. Save and close the file when you are finished.

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

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

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

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

vm.vfs_cache_pressure=50

Save and close the file when you are finished.

@Kaleb-Etele79
Copy link

Hi. Since I'm new with this system how do I save the process in terminal? Thank you in advance.

@davidisnotnull
Copy link
Author

Hey @Kaleb-Etele79, in the nano text editor, you have to press Ctrl + X to exit once you've made your changes. It will prompt you to save changes before exiting, and then that should write everything out to the file.

@Kaleb-Etele79
Copy link

Thank you for the help, appreciate it.

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