None

Swapping Swapfiles

2023-10-12
4 minutes

Swap (or the Page file in Windows) is a way of taking unused disk space and turning it into RAM. It's significantly slower than RAM, but it's definitely there and usable. Configuring a swapfile is as simple as 3 commands on Linux, or a few clicks on Windows. Swap helps in environments with fluctuating memory workloads, or requirements beyond their hardware, to smooth out demand and not crash (or be reaped by the OOM killer).

Making a swapfile is simple:

Bash Session
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048  # 1M * 2048 = 2GB
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon /swapfile

To make the change permanent, add it to your /etc/fstab:

/etc/fstab
/swapfile               none            swap            sw              0 0

#Swap files vs swap partitions

As a quick tangent, let's talk about swap files vs partitions. Whilst not applicable on Windows, Linux lets you use a partition as swap, as opposed to a file.

There's very little difference. Both are provided by the same tooling, the same (or at least incredibly similar) codepaths, and exhibit very similar performance. If you're using a hard drive (still?!), you can see a slight performance boost using a partition, as it ensures all the blocks are closer together (which could be made even faster by Short Stroking). In SSDs, the difference is minimal at best. If you can use a partition, you might as well, but if you can't, you're not missing much. In both cases, swapfiles consume their maximum size constantly - they're not lazy. If you want up to 4GB stored in a swapfile, it'll take up 4GB on disk. The same is also and more clearly true for a partition.

#Growing / shrinking your swapfile

Ah right, the original topic...

As your needs change, you may need to resize your swapfile. Perhaps your website is becoming more popular, and you need more RAM for the web server to run in. Or perhaps you need more disk space to deploy another application.

And herein lies the problem. All the time a swapfile is being used, it can't be resized or modified. In much the same way that removing a stick of RAM from your computer whilst it's running will lead to a very bad time. Instead, we need to carefully plan how to do this, and choose an approach which matches our specific constraints.

#Option 1: Lazy growth

If you are:

  • Feeling lazy
  • Need more swap, not less
  • What minimal intervention and risk

Just make another swapfile. The swap subsystem is completely capable of managing multiple swapfiles without issue. If your current swapfile is at /swapfile, just make /swapfile2. The creation steps are identical for creating a second.

<note>

If you were thinking of having one file on an SSD, and one on a HDD, and having swap tier the swap intelligently, I'm afraid you're out of luck. By default, the first file is used, followed by the second. This can be managed through the file's Priority.

</note>

To view the swapfiles the system knows about, run swapon -s:

Bash Session
$ swapon -s
Filename                                Type            Size            Used            Priority
/swapfile                               file            8388604         3586024         -2
/swapfile2                              file            2097148         0               -3

Now, you have more swap, without needing to touch the original swapfile. It's not exactly clean, as you now have 2 independent swapfiles, but it's definitely the quickest, letting you get back to whatever it is you were (or should be) doing.

#Option 2: Hard replacement

If you wanted to reduce the size of your swap, you can't make an additional file, you need a new one. Achieving this is simple:

  1. Disable the swapfile: swapoff /swapfile
  2. Overwrite /swapfile with a new file the size you want it to be (dd ...)
  3. mkswap and swapon the new file

This is probably the option most people would think of if they want to resize their swap, but it comes at a cost: More memory usage. When you run swapoff, the contents of the swapfile needs to go somewhere. The obvious answer: back to RAM. If you don't have enough RAM spare (highly likely given you're using a swapfile), the swapoff process will fail. Chances are, if you're using a swapfile, you probably don't have the RAM to spare.

<aside>

Rather unhelpfully, swapoff attempts to load the swapfile's contents into RAM before it fails. If a sudden influx of memory usage is going to be an issue, maybe don't attempt this approach.

</aside>

#Option 3: Swap the swap

If you want to modify your swapfile allocation, but don't have enough spare RAM, you can make a temporary swapfile to hold it.

This approach obviously only works if you have the disk space required to handle another swapfile. However, most importantly, doesn't require spare RAM space to temporarily hold the memory.

  1. Make a new, temporary swapfile. It'll need to be at least the the size of the amount of swap you're using currently
  2. Disable the original swapfile with swapoff
  3. Resize the original swapfile as needed (dd ...)
  4. Enable the new swapfile (mkswap and swapon)
  5. Disable the temporary swapfile with swapoff
  6. Remove the temporary swapfile (rm, or perhaps shred if you're concerned)

Yes, it's a lot of steps, and requires a lot of disk space. But depending on how your system is constrained, it may be your best option.

#Option 4: Chaotic evil

If you, unlike me, don't care where your swap file is or what it's called, you can just make a new one and remove the old one, without needing to go through the rotation steps above. Again, this requires enough disk space to temporarily handle both swapfiles, but has the benefit of requiring much fewer steps.

  1. Make a new swapfile
  2. Remove the old one
  3. ???
  4. Profit

Hopefully you won't need to do this too often - /swapfile15 doesn't have a nice ring to it.

Share this page

Similar content

View all →

None

Wiping Hard Drives

2020-11-21
3 minutes

People say there’s no 100% reliable way to wipe a storage drive, and they’re right. By the nature of how mechanical drives work, there’s no real way to say for sure whether the data is ever really gone. With drives, the only way to be sure the content is gone…

Using Scrutiny to monitor your drives

2020-09-24
2 minutes

After recently deploying a ZFS pool, I realized I had little insight into the health of my drives. I can run SMART stats now and then, but that’s not quite the same.Scrutiny Scrutiny is a tool to help you with just that. It presents a web UI which shows you…

Opening Port 22

2018-01-23
2 minutes

My university has a development sever, which it uses to host our coursework without the need to set up a development environment locally. It also enables lecturers to mark our work in a controlled environment, without needing to spin up an environment, and run untrusted code on their machines, a…