Ubuntu Server 18.04 – Managing swap

How to manage remote IIS on Windows Server 2019

Several times in this chapter, I mentioned the swap partition but have yet to give it a formal discussion. Swap is one of those things we never want to use, but always want to make sure is available. There’s even some debate between administrators on whether or not swap is still relevant today. It’s definitely relevant, regardless of what anyone says, as it’s a safety net of sorts.

So what is it? Swap is basically a partition or a file that acts as RAM in situations where your server’s memory is saturated. If we manage a server properly, we hope to never need it, as swap partition is stored on your hard disk which is orders of magnitude slower than RAM. But if something goes wrong on your server and your memory usage skyrockets, swap may save you from having your server go down. It’s a good idea to have it, and considering that hard drive space is cheaper nowadays, there’s really no reason not to.

The way swap is implemented in Ubuntu has changed a bit since the time the first edition of this tutorial was published. With Ubuntu 16.04 and earlier, a swap partition was automatically created for you if you chose the default partitioning scheme during installation, and if you didn’t create a swap partition when partitioning your server, the installer would yell at you for it. However, a swap partition is no longer created by default in modern versions of Ubuntu, and the installer will create a swap file (rather than a partition) for you automatically. You may still see swap partitions on older installations, but going forward, this is the best way to handle it. If you need larger swap, you can delete the swap file and recreate it. That’s definitely an easier thing to do than having to resize your partition tables to enlarge swap, which is potentially dangerous if you make a mistake during the process. Therefore, I’m not going to talk about creating a swap partition in this edition of the tutorial, as there’s no reason to do so anymore.

Although I did say that you should definitely include a swap partition on your server, there is an exception to this rule, though. When it comes to the Raspberry Pi, you should never have swap space if you can help it. Traditionally, the Raspberry Pi images do not include a swap partition or file. You could create one, but I recommend that you don’t. The reason for this is because the Pi uses SD cards for storage, which are orders of magnitude slower than traditional hard disks. While the Raspberry Pi might seem fast enough with regards to I/O when performing average tasks, swap would be so slow on these devices that it would just be more trouble than it’s worth and end up working against you (your Pi would start running so slow it would essentially be unresponsive). Since almost no one runs mission-critical workloads on the Raspberry Pi, the absence of a swap file shouldn’t affect you on that platform.

As you probably noticed from our earlier discussion, the swap file is declared in our /etc/fstab file. In most cases, you would’ve had a swap file created for you during installation. You could, of course, add a swap partition later if for some reason you don’t have one. In the case of some cloud instance providers, you may not get a swap file by default. In that situation, you would create a swap file (which we will discuss later) and then use the swapon command to activate it:

sudo swapon -a

Also, you’d create an entry in the fstab file for this swap file to ensure it gets activated every time the server is started.

When run, the swapon -a command will find your swap partition in /etc/fstab, mount it, and activate it for use. The inverse of this command is swapoff -a, which deactivates and unmounts your swap partition. It’s rare that you’d need to disable swap, unless of course you were planning on deleting your swap file in order to create a larger one. If you find out that your server has an inadequate swap partition size, that may be a course of action you would take.

When you check your free memory (hint: execute free -m), you’ll see swap listed whether you have it or not, but when swap is deactivated, you will see all zeros for the size totals.

So, how do you actually create a swap file? To do so, you’ll first create the actual file to be used as swap. This can be stored anywhere, but /swapfile is typically ideal. You can use the fallocate command to create the actual file:

sudo fallocate -l 4G /swapfile

Here, I’m creating a 4 GB swap file, but feel free to make yours whatever size you want in order to fit your needs. Next, we need to prepare this file to be used as swap:

sudo mkswap /swapfile

Now, we have a handy-dandy swap file stored in our root filesystem. Next, we’ll need to mount it. As always, it’s recommended that we add this to our /etc/fstab file. What follows is an example entry:

/swapfile   none   swap   sw   0 0

From this point, we can activate our new swap file with the swapon command that I mentioned earlier:

sudo swapon -a

Now, you have a handy-dandy swap file to use in a situation where your server runs out of memory. While I certainly hope you won’t need to resort to using swap, I know from experience that it’s only a matter of time. Knowing how to add and activate swap when you need it is definitely a good practice, but for the most part, you should be fine as long as you created an adequate swap partition during installation. I always recommend a bare minimum of 2 GB on servers, but if you can manage to create a larger one for this purpose, that’s even better.

Comments are closed.