A Linux swap setup takes less than two minutes: create a file, set permissions, run mkswap and swapon, add one line to /etc/fstab. You will find that recipe everywhere. What most guides skip are the places where it fails quietly: a swap file the kernel rejects, an fstab entry that does nothing after a reboot, a file you enlarged that stays the same size as swap, and a swapoff that kills your program.
We measured all of it on December 1, 2026 on our own development server (Ubuntu 24.04.5, kernel 6.8, ext4, 24 GB RAM). The tests used throwaway files in /tmp and programs locked into their own memory limit with systemd. The server’s real swap stayed untouched. Three results up front:
1. On swap instead of RAM, our test program ran 35 times slower (16.3 s instead of 0.46 s). Without swap, the OOM killer ended it after 0.15 s. Swap saves processes. It does not make them fast.
2. A swap file you enlarge stays exactly as small as before until you run
mkswapagain.swaponstill reports success.3.
swapoffcan trigger the OOM killer. In our test it pulled 358 MB back from swap, and the kernel killed the program that owned that memory to make room.
If you are not sure how much memory your server needs in the first place, start with How much RAM does a server need?. It also explains why a full swap on its own is not an alarm.
The video (July 2026) walks through the basic steps in a terminal. Below is what we found on top of that when we measured it ourselves.
Linux swap setup: the short version
On Ubuntu, Debian and most other distributions with ext4 or XFS, these commands are all you need. We create a 2 GB file here:
# 1. Check whether swap is already active
swapon --show
free -h
# 2. Create the file (2 GB)
sudo fallocate -l 2G /swapfile
# 3. Permissions: only root may read and write
sudo chmod 600 /swapfile
# 4. Format it as swap
sudo mkswap /swapfile
# 5. Turn it on
sudo swapon /swapfile
# 6. Make it permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# 7. Check that the fstab entry works
sudo swapoff /swapfile && sudo swapon -a && swapon --show
That is it. The rest of this article explains why each step is the way it is and what happens if you do one of them differently. On Btrfs, step 2 does not work like this. More on that below.
What is swap?
Swap is disk space that Linux uses as an extension of RAM. When memory gets tight, the kernel writes memory pages nobody is using to disk and reads them back as soon as a program touches them again. A page is a 4 KB block.
That sounds like an emergency measure, but in daily operation it is more like housekeeping. Many programs allocate memory at startup that they never touch again. If that memory sits in swap, there is more room in RAM for the page cache, the copies of files Linux keeps in memory so the next read does not have to hit the disk.
On our development server it looks like this:
total used free shared buff/cache available
Mem: 23456 6267 2643 109 15058 17189
Swap: 4095 3880 215
3.8 of 4 GB swap in use while 17 GB of RAM is available. If that makes you nervous, you are reading the number wrong. The biggest items in swap were a MySQL instance with 465 MB, a Node process with 444 MB and a Java service with 240 MB. Those processes have been running for days and have not touched part of their memory since startup. Memory pressure (/proc/pressure/memory) at the same moment was 0.00. No process had to wait for memory.
Swap does two jobs:
- Buffer against memory spikes. A build, a backup, a database query that briefly needs a lot of memory. Without swap, the OOM killer steps in and ends a process.
- Storage for cold memory. Memory that is never used again moves to disk and frees RAM for the cache.
What swap is not: a substitute for too little RAM. A server that permanently depends on swap gets slow. We measured how slow.
Swap file or swap partition?
A dedicated swap partition used to be standard. Today Ubuntu (since 17.04) and many cloud images create a swap file, usually /swapfile. Our server has exactly that.
| Swap file | Swap partition | |
|---|---|---|
| Resize | any time, no reboot | move partitions, usually with downtime |
| Add on a running server | yes | only with unpartitioned disk space |
| Speed on ext4/XFS | practically the same | practically the same |
| Btrfs | works, with special rules | no issues |
| Hibernation | works, needs an offset | simpler |
On a server, the file is almost always the better choice. You can create, grow or remove it at any time without touching the partition table. The speed difference on ext4 and XFS has been a non-issue for years: when swap is enabled, the kernel looks up once where the file’s blocks are and then writes directly to disk.
A partition mostly makes sense on laptops that hibernate or when you are repartitioning anyway.

Step 1: Check whether swap already exists
Before creating anything, check what is running:
swapon --show
free -h
cat /proc/swaps
If swapon --show prints nothing, no swap is active. Many cheap VPS ship exactly like that: 1 or 2 GB RAM and no swap. On our server it showed:
NAME TYPE SIZE USED PRIO
/swapfile file 4G 3.8G -2
Also check /etc/fstab for a swap entry that just isn’t active right now:
grep -i swap /etc/fstab
Step 2: Create the file, fallocate or dd?
There are three ways to create a file of a fixed size. We tried all three with 1 GB on ext4:
| Command | Time | Space used on disk | Usable as swap? |
|---|---|---|---|
fallocate -l 1G | 0.00 s | 1.1 GB | yes |
dd if=/dev/zero bs=1M count=1024 | 0.77 s | 1.0 GB | yes |
truncate -s 1G | 0.00 s | 0 bytes | no |
fallocate only reserves the blocks without writing them. On ext4 and XFS it is the fastest option and works for swap. dd actually writes zeros and is correspondingly slower for large files. At 8 GB you wait a few seconds, on slow disks much longer.
truncate creates a sparse file. It looks like 1 GB from the outside but occupies no blocks at all. ls -l shows 1.0 GB, du shows 0. The kernel rejects it, and says so clearly:
swapon: /tmp/gmswap/sp.swap: skipping - it appears to have holes.
Exit code 255. The same happens if you copy a swap file with a cp that creates holes, or use a file system that only pretends to support fallocate. If swapon complains about holes, use dd.
Rule of thumb: fallocate on ext4 and XFS, dd whenever you are unsure, and truncate never.
Step 3: Permissions, and why 644 is only a warning
Everything that was in memory ends up in swap: passwords, session keys, database contents. If every user can read the file, they can search it. So it belongs to root, and only root may read it:
sudo chmod 600 /swapfile
We wanted to know what happens if you forget. fallocate creates the file with mode 644, readable by everyone. swapon then prints a warning:
swapon: /tmp/gmswap/fa.swap: insecure permissions 0644, 0600 suggested.
And enables the file anyway. Exit code 0, swap active. If your setup script only checks the exit code, you will never notice. At boot, the warning ends up somewhere in the journal where nobody reads it.
Check this once on your existing servers:
ls -l /swapfile
# -rw------- 1 root root ... /swapfile ← correct
Steps 4 and 5: mkswap and swapon
mkswap writes a small header at the start of the file so the kernel recognizes it as swap. Then swapon enables it:
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
mkswap prints the size and a UUID. You don’t need the UUID for a file, but you do for a partition.
Multiple swap areas and priorities
You can have several swap areas at once. The kernel uses the one with the highest priority first. Without a value, it assigns negative numbers in descending order: our existing /swapfile had -2, the second test file automatically got -3.
sudo swapon -p 10 /swapfile2
With -p 10, our test file was used before the existing swap. That is useful if you have one swap on fast NVMe and one on a slow disk. If two areas have the same priority, the kernel spreads pages across both in turn, similar to RAID 0.
Step 6: The fstab entry
swapon only lasts until the next reboot. To bring swap back after that, it belongs in /etc/fstab:
/swapfile none swap sw 0 0
With a priority:
/swapfile none swap sw,pri=10 0 0
Our server has exactly the first line.
Important: back up fstab first. A typo in another line of this file can stop the boot process.
sudo cp /etc/fstab /etc/fstab.bak
Step 7: Test the entry without rebooting
Most guides stop after the fstab entry. Then you only find out at the next reboot whether it works. We added a typo on purpose and tested with a copy of fstab. swapon has a -T flag that reads a different file instead of /etc/fstab:
# fstab.typo contains: /tmp/gmswap/dd.sawp none swap sw 0 0
sudo swapon -a -v -T fstab.typo
# swapon: cannot open /tmp/gmswap/dd.sawp: No such file or directory
# exit code 255
The correct entry (dd.swap instead of dd.sawp) went through with exit code 0 and the priority from pri=7.
On the real system you test like this:
sudo swapoff /swapfile
sudo swapon -a
swapon --show
If the file shows up again, the entry is correct. If not, the reason is in the output. On systemd systems, systemd creates a separate unit from every swap line in fstab, visible with systemctl list-units --type swap. A boot-time error shows up there as failed, but the system still boots. That is exactly why a broken swap entry often goes unnoticed for months.
How big should swap be?
The old rule “twice as much swap as RAM” comes from a time when servers had 256 MB of memory. Today it makes no sense. With 64 GB RAM you don’t need 128 GB of swap.
Our recommendation for servers without hibernation:
| RAM | Swap | Why |
|---|---|---|
| up to 2 GB | 1 to 2 GB | small VPS otherwise hit the OOM killer on every apt upgrade or build |
| 2 to 8 GB | 2 GB | enough buffer for spikes, little wasted disk |
| 8 to 32 GB | 2 to 4 GB | mainly holds cold memory |
| over 32 GB | 4 GB, often less | more rarely helps, better check why so much is being swapped |
Hibernation is practically never relevant on servers. On a laptop, swap for hibernation needs at least as much space as RAM.
Our development server has 24 GB RAM and 4 GB swap. The swap is almost full, and that is fine. A Shopware production server we look after had 64 GB RAM and 16 GB swap when we measured it in September 2026, with 8 MB in use. The 16 GB are simply too much there, but they don’t hurt either, apart from disk space.
More important than the size is whether your server needs swap at all to stay alive. /proc/pressure/memory tells you that better than any table. More on that in the section about memory pressure.

Swappiness: what the value really controls
vm.swappiness is the knob every guide mentions. The default is 60, on our server too.
cat /proc/sys/vm/swappiness
The common explanation goes: “At 60, Linux starts swapping when 60% of RAM is full.” That is wrong. Swappiness is not a fill-level percentage. It tells the kernel how expensive it is to swap out program memory compared to shrinking the file cache. It ranges from 0 to 200:
- low (0 to 10): the kernel almost only drops file cache and swaps program memory only when there is no other way.
- 60: balanced, default.
- 100: both count as equally expensive.
- above 100: only useful when swap is much faster than the disk, for example with zram in RAM.
Our test: what the kernel reclaims first at 60
We locked a program into a 1 GB memory limit. It first read a 400 MB file, which then sat in the file cache, and then allocated 200 MB of its own memory. We then asked the kernel twice, via the control group’s memory.reclaim file, to free 100 MB:
| Point in time | File cache | Program memory | Swap |
|---|---|---|---|
| Start | 400 MB | 204 MB | 0 MB |
| after freeing 100 MB | 300 MB | 204 MB | 0 MB |
| after another 100 MB | 200 MB | 204 MB | 0 MB |
At swappiness 60 and with plenty of file cache, the kernel did not swap out a single byte of program memory. It only shrank the cache. Swap comes into play once the cache gets smaller or program memory has been idle for a long time.
That matches our server: 3.8 GB in swap accumulated over days, piece by piece, from memory nobody touches.
Changing swappiness
Temporarily:
sudo sysctl vm.swappiness=10
Permanently:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system
When is it worth it? For database servers that manage their own cache (MySQL with a large buffer pool, PostgreSQL with shared_buffers), many recommend 10 so the database cache does not get swapped out. On a mixed server with a web server, a few Node services and cron jobs, 60 is usually right. We keep ours at 60 because memory pressure is zero and there is nothing to fix.
A value of 0 does not mean “swap off”. The kernel then only swaps when it would otherwise have to kill a process. If you really want swap off, disable it with swapoff.
vfs_cache_pressure
A second value often shows up in the same guides: vm.vfs_cache_pressure, default 100. It controls how quickly the kernel drops directory and inode information from the cache. Many recommend 50. We have never changed it and never found a reason to. Without a measurable bottleneck, leave it alone.
What swap really costs: 35 times slower
Now the measurement most guides leave out. We wrote a small Python program that allocates 600 MB and then walks through all of it three times. We started it with systemd-run, which gives it its own memory limit without affecting the rest of the server:
systemd-run --scope -p MemoryMax=200M -p MemorySwapMax=1G python3 alloc.py 600 3
Three runs:
| Limit | Allocate (600 MB) | 3 passes | Total | Result |
|---|---|---|---|---|
| 1 GB RAM, no swap needed | 0.35 s | 0.03 s | 0.46 s | completes |
| 200 MB RAM, no swap | – | – | 0.15 s | killed by the OOM killer |
| 200 MB RAM + 1 GB swap | 4.51 s | 11.53 s | 16.25 s | completes, 416 MB in swap |
With swap, the program was 35 times slower than with enough RAM. The passes alone were about 380 times slower, because every pass had to read pages back from disk and write others out. And that is on an SSD-backed virtual disk at a cloud provider, not on an old hard drive.
Without swap there is no choice between fast and slow. There is only: runs or gets killed. Our program was shot down with signal 9 after 0.15 seconds.
That is the honest trade-off:
- With swap, a process survives a memory spike but slows to a crawl.
- Without swap, it dies immediately and the rest of the server stays fast.
For most servers, a small swap is the better choice. A short build that takes 30 seconds longer beats an aborted build. A database server that swaps permanently, on the other hand, needs more RAM, not more swap.

Measure memory pressure, not swap usage
Whether your server needs swap or suffers from it is not something free -h tells you. Pressure Stall Information does, available since kernel 4.20:
cat /proc/pressure/memory
some avg10=0.00 avg60=0.00 avg300=0.00 total=655567162
full avg10=0.00 avg60=0.00 avg300=0.00 total=641290752
some avg60 is the share of the last 60 seconds in which at least one process had to wait for memory. Our server: 0.00, despite nearly full swap. Our rule of thumb from operations:
- below 1: all good
- 1 to 10: keep an eye on it
- above 10: too little RAM, swap now only helps you survive
fullconstantly above 0: the server is noticeably stalling
It is also worth looking at actual swap activity:
vmstat 1 10
The si (swap in) and so (swap out) columns show how many KB per second are moving right now. Constant values above zero mean swap is actually working. Zeros mean swap only holds cold memory, and that is fine.
To find which processes sit in swap:
for p in /proc/[0-9]*; do
s=$(awk '/VmSwap/{print $2}' $p/status 2>/dev/null)
[ -n "$s" ] && [ "$s" -gt 10000 ] && echo "$s kB $(cat $p/comm)"
done | sort -rn | head
For continuous monitoring, these values belong in your monitoring system. How we built ours is in Server monitoring.
Growing swap: the mkswap trap
At some point 2 GB is not enough. We tried what happens when you change the file while it is in use:
| Action on active swap | Result |
|---|---|
fallocate -l 512M | “Text file busy”, refused |
dd … conv=notrunc | “Text file busy”, refused |
mkswap | refused: “unable to erase bootbits sectors” |
truncate -s 128M (shrink) | “Text file busy”, refused |
truncate -s 512M (grow) | goes through, file is 512 MB afterwards |
The kernel protects an active swap file against almost everything. It only allows growing via truncate. That changes nothing, though: swap stays the same size, and the new bytes are a hole in the file.
The real trap comes next. We turned the 256 MB file off, grew it to 512 MB with fallocate and turned it back on without mkswap:
NAME TYPE SIZE USED PRIO
/tmp/gmswap/t.swap file 256M 0B -3
The file was 512 MB, the swap 256 MB. swapon returned exit code 0. The size is stored in the header that mkswap wrote, and that header does not know about the new file size. Only after running mkswap again did swapon --show show 512 MB.
The correct sequence:
sudo swapoff /swapfile
sudo fallocate -l 4G /swapfile # or dd
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
For a file, mkswap generates a new UUID the second time. If your fstab uses the file path, that doesn’t matter. If it uses UUID=…, you need to update it.
swapoff can kill programs
The sequence above needs swapoff. And swapoff is not as harmless as it looks. It has to bring everything in swap back into RAM. If there is no room, the OOM killer steps in.
We reproduced that: a program limited to 150 MB of RAM with 500 MB allocated. 358 MB of it sat in our test swap file. Then:
swapoff /tmp/gmswap/fa.swap
# 1.51 s, exit code 0
swapoff reported success. The kernel log said:
swapoff invoked oom-killer: gfp_mask=0x100cca(GFP_HIGHUSER_MOVABLE), order=0, oom_score_adj=1000
Memory cgroup out of memory: Killed process 1763056 (python3) ...
Swap was off, and the program whose memory had been in it was dead. In our test the limit was artificially tight. On a real server, the same thing happens when free RAM is smaller than the swap in use.
So before every swapoff:
free -m
If available is clearly larger than Swap used, you can turn it off safely. On our development server with 3.8 GB in swap and 17 GB available, it would be no problem, just a few seconds of work. On a small VPS with 1 GB RAM and 900 MB of swap in use, don’t do it, or enable a second swap file first before turning off the old one.
The safe way on tight servers: create a new, larger file, enable it, then disable and delete the old one. Memory moves from the old file into the new one, not into RAM.
sudo fallocate -l 4G /swapfile2
sudo chmod 600 /swapfile2
sudo mkswap /swapfile2
sudo swapon /swapfile2
sudo swapoff /swapfile
sudo rm /swapfile
# switch fstab to /swapfile2
Swap on Btrfs
If you use Btrfs, for example on Fedora, openSUSE or a NAS, the short recipe fails. We created a Btrfs file system in an image file and tried fallocate:
swapon: /tmp/gmswap/bt/s.swap: swapon failed: Invalid argument
Btrfs is a copy-on-write file system. The kernel needs swap blocks that stay in fixed places, which copy-on-write does not guarantee. Since btrfs-progs 6.1 there is a dedicated command:
sudo btrfs filesystem mkswapfile --size 2g /swap/swapfile
sudo swapon /swap/swapfile
That worked immediately for us (btrfs-progs 6.6.3). lsattr then showed the C (No-CoW) attribute on the file, which the command sets itself. Further limits: the file must not live in a subvolume that gets snapshotted, and must not be compressed. A dedicated /swap subvolume is the usual approach.
With older btrfs-progs: create an empty file, set chattr +C, then fill it with dd. The order matters because the attribute only takes effect on empty files.
Swap in Docker containers and with systemd
Containers and systemd services can have their own swap limits. We used that in our tests, and it is useful in production too:
# in a systemd unit under [Service]
MemoryMax=1G
MemorySwapMax=0
That way a service cannot swap and gets killed when it crosses the limit, instead of dragging the whole server into slow motion. That can be right for a service with a fixed memory footprint, like a database. How to write such units is in Create a systemd service.
In Docker the options are --memory and --memory-swap. Careful: --memory-swap is the sum of RAM and swap, not swap alone. --memory=1g --memory-swap=1g therefore means: no swap. If you only set --memory, the container gets the same amount again as swap on top.
zram and zswap: compressed swap in RAM
Two alternatives often come up in the same breath:
- zram creates a compressed block device in RAM that serves as swap. Fedora has used it as the default since version 33. On small devices with slow disks it helps noticeably.
- zswap is a compressed cache in front of regular swap. Pages first land compressed in RAM and only go to disk when that buffer is full.
On our server, zswap is present but disabled (/sys/module/zswap/parameters/enabled = N, compressor lzo, at most 20% of RAM). The zram module is missing from this kernel entirely. So we did not measure either, and we won’t give you a number we don’t have.
What we can say without a measurement: on a server with 1 or 2 GB RAM and a slow disk, zram or zswap is worth a try. On a server with enough RAM where swap only holds cold memory, it brings little.
Removing swap
If you really want to get rid of swap:
free -m # first check there is enough free RAM
sudo swapoff /swapfile
sudo rm /swapfile
sudo sed -i.bak '/\/swapfile/d' /etc/fstab
swapon --show # should be empty
The -i.bak flag keeps a backup of fstab. Without removing the fstab line, systemd reports a failed swap unit at the next boot. The server still boots, but you get a red line in systemctl --failed that will scare someone eventually.
Common errors and what they mean
| Message | Cause | Fix |
|---|---|---|
skipping - it appears to have holes | sparse file (truncate, cp) | recreate the file with dd |
swapon failed: Invalid argument | usually Btrfs or a file system without swap support | btrfs filesystem mkswapfile |
insecure permissions 0644, 0600 suggested | permissions forgotten | chmod 600, works anyway |
read swap header failed | mkswap forgotten | run mkswap |
Text file busy | file is currently active swap | swapoff first |
swapon --show shows old size | mkswap forgotten after growing | swapoff, mkswap, swapon |
| no swap after reboot | fstab entry missing or typo | swapon -a -v |
Linux swap setup: the checklist

swapon --showandfree -h: is there swap already?- Pick a size: 1 to 2 GB on small servers, 2 to 4 GB otherwise.
fallocateon ext4/XFS,ddwhen unsure,btrfs filesystem mkswapfileon Btrfs.chmod 600, even thoughswaponworks without it.mkswap,swapon,swapon --show.- Back up fstab, add the entry, then test with
swapoff+swapon -a. - Only change swappiness if you have a reason. 60 is right for mixed servers.
- Watch memory pressure in
/proc/pressure/memory, not swap usage. - Before every
swapoff:availablemust be larger thanSwap used. - After every resize: don’t forget
mkswap.
If you are setting up a new server from scratch, swap is one of the first steps. We describe the whole process in Linux server setup. And if swap fills up your disk or you need room, Linux disk full helps.
The second video (August 2026) explains the difference between a swap file and a swap partition with terminal examples.
Frequently asked questions about Linux swap setup
How do I set up swap on Linux?
Create a file (fallocate -l 2G /swapfile), set permissions (chmod 600), format it (mkswap /swapfile), enable it (swapon /swapfile) and add the line /swapfile none swap sw 0 0 to /etc/fstab. Then check with swapoff /swapfile && swapon -a that the entry works. On Btrfs, use btrfs filesystem mkswapfile instead.
How much swap do I need?
On servers without hibernation, 1 to 4 GB is usually enough regardless of installed RAM. Small VPS with 1 to 2 GB RAM should have at least as much swap as RAM. The “twice your RAM” rule is outdated. For hibernation on a laptop you need at least as much swap as RAM.
Is swap bad for SSDs?
Not noticeably. Modern SSDs handle hundreds of terabytes of writes. A server that only swaps out cold memory writes little. It only becomes an issue if the server swaps constantly (vmstat shows ongoing si/so values). But then the SSD is not the problem, too little RAM is.
What does swappiness 60 mean?
It does not mean Linux starts swapping at 60% RAM usage. The value weights how willing the kernel is to swap out program memory instead of shrinking the file cache. At 60 it prefers to drop cache. In our test, with plenty of cache, it did not swap out a single byte of program memory.
Should I set swappiness to 10?
On database servers with a large cache of their own, often yes. On mixed servers with a web server and several services it rarely helps. Only change it if you have a measurable reason, such as a database cache ending up in swap and slowing queries down.
Why is my swap full even though there is free RAM?
Because the kernel proactively swaps out memory that hasn’t been used for a long time to make room for the file cache. Our server has 3.8 GB in swap and 17 GB of RAM available at the same time. As long as /proc/pressure/memory stays near zero and vmstat shows zeros under si/so, that is not a problem.
Can I increase swap without rebooting?
Yes. Either turn off the old swap with swapoff, grow the file, run mkswap again and turn it back on. Or, safer on tight servers: create and enable a second file, then disable the old one. Without running mkswap again, swap stays as small as before even if the file is larger.
Is swapoff dangerous?
It can kill programs. swapoff moves everything from swap back into RAM. If there is not enough room, the OOM killer steps in. In our test, swapoff reported success and the program that owned the swapped memory was dead. Check with free -m first that available is larger than the swap in use.
Swap file or swap partition?
On servers, almost always the file. You can create, grow and remove it without repartitioning, and on ext4 and XFS it is as fast as a partition. A partition mainly makes sense on laptops that hibernate.
Does a swap file work on Btrfs?
Yes, but not with fallocate. That gave us “Invalid argument”. btrfs filesystem mkswapfile --size 2g /swap/swapfile creates the file correctly with the No-CoW attribute. The file must not be compressed and must not live in a subvolume that gets snapshotted.
Do I still need swap on a server with 64 GB RAM?
A small swap of 2 to 4 GB does no harm and holds cold memory. Our Shopware server with 64 GB RAM had 16 GB swap when we measured it in September 2026, with 8 MB in use. More important there is monitoring that alerts you when memory pressure rises.
Which is better: zram or a swap file?
On small systems with slow disks, zram is often faster because compressed RAM beats any disk. On servers with enough RAM that only swap out cold memory, the difference is small. We did not test zram on our server because the module is missing from the kernel.
Conclusion: Linux swap setup is easy, the traps come afterwards
The seven setup commands are quick to type. What really matters are the things that produce no error: wrong permissions that only trigger a warning, a grown file without mkswap, an fstab entry nobody tested, and a swapoff that reports success while the kernel kills a program.
Swap does not make your server faster. It makes it more robust against spikes and gives cold memory somewhere to go. For everything else you need RAM. In our test, a program on swap was 35 times slower than without a memory limit, but it ran. Without swap it was dead after 0.15 seconds.
Our recommendation: 2 to 4 GB as a file, chmod 600, leave swappiness at 60, and monitor memory pressure instead of swap usage. That is all a typical server needs.
