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:
$ 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
:
/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
:
$ 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:
- Disable the swapfile:
swapoff /swapfile
- Overwrite
/swapfile
with a new file the size you want it to be (dd ...
) 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.
- Make a new, temporary swapfile. It'll need to be at least the the size of the amount of swap you're using currently
- Disable the original swapfile with
swapoff
- Resize the original swapfile as needed (
dd ...
) - Enable the new swapfile (
mkswap
and swapon
) - Disable the temporary swapfile with
swapoff
- 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.
- Make a new swapfile
- Remove the old one
- ???
- Profit
Hopefully you won't need to do this too often - /swapfile15
doesn't have a nice ring to it.