Live data from Hacker News

Stop making swap partitions—use swap files instead

gist.github.com

131–140 of 257 posts

Re: Stop making swap partitions—use swap files instead

#131
post #94
post #86

Use zram instead. This from crappy 8G RAM multimedia/browsing minipc I'm on right now. The SSD is quite slow and browser is memory hungry so it was an improvement from on disk swap. $ zramctl NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 zstd 3G 482.9M 86.7M 91.8M 4 [SWAP]

zram is a good choice when data in RAM is very repetetive/compressible. Web browsers is one such load. Android apps is another good one. When that data doesn't compress well at all, zram is no help and traditional swap-to-storage is a better choice. And there is a middle-ground option, zswap: swapped out pages will eventually end up on a block device, but are compressed first.

Zram also supports a block device for pages that do not compress. So the primary advantage of zswap is that one can use its swap for hibernation while it does not work with zram.

Re: Stop making swap partitions—use swap files instead

#132
post #80

Tangential, but does anyone know why in 2026 and on Debian 13, my machine still hangs when some process exhausts RAM? Is there really no higher-priority kernel process to prevent total freeze of the system and send a SIGKILL to the culprit process when such a scenario happens?

Don't worry, it's not just you: https://lkml.org/lkml/2019/8/4/15 It's because linux is a toy OS. Specifically, it overcommits memory in the hope/assumption that it won't all be used at once, but doesn't have a way to gracefully degrade when applications collectively want to use more memory(+swap) than it actually has. You can turn off overcommit, but applications are designed with the overcommitting feature in mind,…

Illumos also doesn't overcommit, if you want the more modern OS descended from Solaris.

Re: Stop making swap partitions—use swap files instead

#133

For a while since RAM sizes have gotten so big I would set up linux servers without swap at all. I figured what is a 2GB swap file going to do if the system has already exhausted 128GB of real RAM? I never saw any issues doing this, but I learned that linux ideally wants some swap space, which it will use for some kind of housekeeping stuff.

It does give you some more RAM for free, as Linux can park allocated but rarely accessed memory and use the freed space for other purposes.

Re: Stop making swap partitions—use swap files instead

#134
post #106

> fallocate / chmod / mkswap Why not "mkswap --size ... --file ...", which does these three things and more? For instance, according to the mkswap man page, "[...] sets the nocow attribute for newly created files [...]" which is a detail that seems to be missing from this gist.

I wrote this years ago before LLMs made it easy to validate guesswork. I’ll update! Good call

Re: Stop making swap partitions—use swap files instead

#135

For a while since RAM sizes have gotten so big I would set up linux servers without swap at all. I figured what is a 2GB swap file going to do if the system has already exhausted 128GB of real RAM? I never saw any issues doing this, but I learned that linux ideally wants some swap space, which it will use for some kind of housekeeping stuff.

I have tried to forego swap because I had hoped that it would avoid swap thrashing, where performance goes into the toilet; I'd rather a process was killed but I could still access the system, than be unable to login to it. But I've found that even with no/little swap, the system can get into high memory pressure situations (even easier because it can't swap out unused pages of libraries and the like), and still go i…

The old rule of thumb used to be 2x actual RAM for your swap file/partition size.

That's probably not right with today's RAM sizes but I'm not sure what the current rule is. I just go with the Ubuntu installer default but I don't know if that's just a fixed number or computed in some way from amount of RAM available.

Re: Stop making swap partitions—use swap files instead

#136
post #80

Earlier quoted context omitted.

Don't worry, it's not just you: https://lkml.org/lkml/2019/8/4/15 It's because linux is a toy OS. Specifically, it overcommits memory in the hope/assumption that it won't all be used at once, but doesn't have a way to gracefully degrade when applications collectively want to use more memory(+swap) than it actually has. You can turn off overcommit, but applications are designed with the overcommitting feature in mind,…

Illumos also doesn't overcommit, if you want the more modern OS descended from Solaris.

can't believe i forgot to mention them! Updating the parent for visibility

Re: Stop making swap partitions—use swap files instead

#138
post #86

Use zram instead. This from crappy 8G RAM multimedia/browsing minipc I'm on right now. The SSD is quite slow and browser is memory hungry so it was an improvement from on disk swap. $ zramctl NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 zstd 3G 482.9M 86.7M 91.8M 4 [SWAP]

On x64 CPU for laptops the memory bandwidth is rather limited and a fast compressor like lz4 can saturate it. As the result latest SSD are faster then z-ram when using hardware encryption on SSD.

If you care about performance you shouldn't be running from swap period. Swap is just a safety net.

Re: Stop making swap partitions—use swap files instead

#139

For a while since RAM sizes have gotten so big I would set up linux servers without swap at all. I figured what is a 2GB swap file going to do if the system has already exhausted 128GB of real RAM? I never saw any issues doing this, but I learned that linux ideally wants some swap space, which it will use for some kind of housekeeping stuff.

It does not need it at all. I’ve run a million+ hosts with no swap. You need to be really disciplined about memory allocations (us cgroups) and ok with what happens if you run out (something in that cgroup dies). But there is no “housekeeping “ that requires it.

Re: Stop making swap partitions—use swap files instead

#140

Tangential, but does anyone know why in 2026 and on Debian 13, my machine still hangs when some process exhausts RAM? Is there really no higher-priority kernel process to prevent total freeze of the system and send a SIGKILL to the culprit process when such a scenario happens?

Linux and the software running on it are generally very bad at handling out-of-memory situations.
Post reply on HN