HACKER Q&A
📣 fernvenue

Do we still need SWAP partition?


Do we still need SWAP partition?


  👤 dveeden2 Accepted Answer ✓
With a 16GiB RAM laptop I often use more memory than that which then ends up on swap (on zram)

With NUMA swap etc. also becomes a bit more complicated (see also https://blog.jcole.us/2010/09/28/mysql-swap-insanity-and-the... )

I think the main question here is to use a swap partition or a swap file.

The main problem of swap partitions is how inflexible they are, but lvm/btrfs/zfs make that less of an issue.

On Solaris a crashdump was written to swap when the system panicked. Not sure what Linux/BSD crashdump systems use nowadays.


👤 mixmastamyk
Need? No. I havent used a swap partition since I first got a machine with 16GB RAM. I write text files for a living and its been plenty.

Every year or so a program (usually firefox) would spring a leak and lock up the machine for a minute or two then the OOM killer would kill it. This would have happened with or without swap; I think it's faster without?

So haven't bothered with partitions for a while. But a couple of years ago, the dists starting adding swap files by default. Ok, I thought, I won't fight it.

I do keep it reasonable, one or two gig. No problems so far. NVME is pretty darn fast, though htop says I don't really use it.

Get enough RAM for your use case, double if on one of those soldered Macs. This served me well, we had a 2013 MBP in great working condition (because I disregarded the salesbot advice and got 16gb) still works great but the recipient wanted a new M2 this year.

Personaly went with the Framework so not worried.


👤 countWSS
NO. Swap is evil. The swap on HDD-based systems kills performance. with SSDs you can afford constant interruptions at cost of faster wearout(especially TLC/QLC). The gaslighting about swap being neccessary/good or fast is strange - most machines run order of magnitude faster without swap once you get a moderate load(even SSD benefit). Some exotic workload need lots of RAM but demanding this swapping be enabled by default is bad, its utility is justified by weird ideas that "some shitware needs swap to work, because it just is so leave it on and don't change it".

👤 yodi
Depend on use case. For me, its very crucial. Context, I'm working on ML and run several processing where usually consume memory up to 1TB at my workstation. Since my RAM only 196GB, this will trigger OOM killer, without Swapfile.

I use HHHL PCI SSD to host my swapfile to withstand with rapid read-write activities


👤 Am4TIfIsER0ppos
Do you hibernate? Then you need swap, and I think it has to be a partition. I do because sleep is broken on this system (it doesn't reliably wake) so I disabled it in systemd to prevent the couple of accidental sleeps I did.

👤 speedgoose
I use some swap on servers because software leaks memory all the time and I’m cheap. Compressed swap can reduce a lot a cloud bill, and the performance impact of storing old unused memory pages on persistent storage is fine.

👤 bjourne
No, we don't. If a process begins swapping 9999 out of 10 000 it is because something has gone wrong and it is better to terminate that process than destabilizing the whole system by using the disk as ram.

👤 bhaney
I never use a swap partition anymore at all. I just create or destroy swapfiles dynamically as I feel I need them.

👤 toast0
Having a big enough swap partition to get a kernel core dump is handy. If you hibernate to swap, that needs to be big enough too. Sometimes swap partition is better than swap file, but a partition is harder to resize.

Personally, I find utility in having something approaching max(512mb, 2xRAM) swap available. If you've got an older system, 2xRAM is a good rule of thumb, but not for larger ram. IMHO, swap usage falls into two categories: slow leak and fast leak. In a slow leak, size doesn't really matter, just that it's being used, you can set your alert at 10% (or 75% or wherever) and you'll hit it eventually regardless of underlying size, in the mean time everything works fine, no big deal or urgency. In a fast leak, the system tends to get really slow really quick, until you hit the swap limit and then things usually get broken and fast; maybe if you get in early you can kill things before it spirals, but chances are you won't, and you'll just need to wait until you hit the max and then pick up the pieces; having a small swap means the time of max swapping is pretty limited.

TLDR: a small swap partition gives you a clear indication of a slow leak, but doesn't prolong agony during a fast leak.

Also note, measuring used memory is hard, but measuring used swap is easy. If your system typically has 0% swap used (which it should!), and starts swapping, you know something changed to use more memory (or the kernel swap algorithms changed)


👤 DamonHD
I run my RPI servers without a swap partition (but with a small amount of zram) to avoid horrible wear on the SD cards.

👤 dusted
I have a 2GiB swap on my 32 GiB worksation, and swappiness set to the lowest level. Why ? Because if I ever reached that situation, things will crawl to a halt and I might as well reboot. Also, 2G is so cheap I'm just paying the cargo-cult entity to not think about it.

On my 64 GiB server, I have 4GiB, same reason.

Kernel has actually swapped out some stuff, probably ubuntu bloat stuff that's never used anyway.