This is a step-by-step tutorial on how to create or remove a swapfile on a Linux machine, and whether you should create swap space in the first place.
Also included is the most correct fstab
entry: most articles get this “wrong”
(but it doesn’t actually matter).
Should you use swap?
A common misconception is that adding swap to your system can reduce performance. However, even if you aren’t running out of RAM, it can still be beneficial to add swap.
See these points according to an article by Hayden James:
Even if there is still available RAM, the Linux Kernel will move memory pages that are hardly ever used into swap space.
It’s better to swap out memory pages that have been inactive for a while, keeping often-used data in cache, and this should happen when the server is most idle, which is the aim of the Kernel.
Avoid setting your swap space too large if it will result in prolonging performance issues, outages, or your response time (without proper monitoring/alerts).
Should you use a swap file or partition?
Swap partitions should be preferred because swapfiles tend to be slower and slightly more complex to set up, especially if hibernating. A swapfile might be preferred due to its flexibility (ease to resize), but if you use LVM then you can easily resize the swap partition anyways.
Tutorial
Step 1: Create the file
The first step is to allocate the file.
consoleCopy
|
|
Replace [size in MiB]
with the size of your swapfile in Mebibytes (MiB). You
can use this online converter to
convert from Gibibytes (GiB, often confused with Gigabytes, but that’s a
misconception for another time) to MiB, which you can put in the command.
Or you can look at this table for common sizes:
GiB | MiB |
---|---|
1 | count=1024 |
2 | count=2048 |
3 | count=3072 |
4 | count=4096 |
8 | count=8192 |
16 | count=16384 |
If you aren’t sure how big your swapfile should be, take a look at Table 15.1 on this Red Hat documentation page. Remember that if your first swapfile isn’t large enough, you can create another one.
On my computer I have 32 GiB of RAM and a 16 GiB swap partition, rather than swapfile, because I can flexibly expand the swap partition using LVM.
Step 2: Change swapfile permissions
The swapfile should only be readable by the system (root
user). Run this
command to change it:
consoleCopy
|
|
Step 3: Format the swapfile
Use the mkswap
command to format the file to be used as swap (basically just
add a header to identify it):
consoleCopy
|
|
Step 4: Create an entry in /etc/fstab
An entry needs to be added to /etc/fstab
for the swapfile to be enabled during
bootup. Open /etc/fstab
in a text editor, and add this line to the end (add
spaces to line up with other entries if you want, whitespace is ignored):
textCopy
|
|
Step 5: Enable the swapfile
Adding the fstab
entry won’t enable the swapfile until a reboot. To enable it
now, use the swapon
command.
consoleCopy
|
|
Finally: check the swap status
Use swapon
and free
to verify that your new swapfile has been added:
consoleCopy
|
|
Removing a swapfile
Step 1: Disable/unload the swapfile
consoleCopy
|
|
Step 2: Remove the entry from fstab
Open /etc/fstab
in a text editor and find the line matching the swapfile you
want to remove, and delete it. For example:
textCopy
|
|
Step 3: Delete the actual file
consoleCopy
|
|
References
Use dd
instead of fallocate
See “Files with holes” under the man page.
The options field in fstab
On Linux, sw
doesn’t mean anything for swapon
, so it gets ignored. See:
Comments
If you provide an email address, you can enable notifications for replies to your comment. It will not be shown publicly.