Live data from Hacker News

Stop making swap partitions—use swap files instead

gist.github.com

241–250 of 256 posts

Re: Stop making swap partitions—use swap files instead

#241

Earlier quoted context omitted.

There are lots of very good reasons why running small root partitions on Linux desktops is a very bad idea. The smart money is on a single large partition for home and root and everything else. This way you get 100% disk availability with zero extra effort and you get maximum performance. No need to shuffle disk space around with resizing partitions or thing volumes or anything like that. Unless you have some sort of…

[flagged]

> In the last decade, keeping my OS on a different volume from my data in `/home` has saved my work at least 5-10 times.

Mine is of course just another anecdote, but despite seeing some crashes or hangs over the years, I can't remember the last time I've even had to manually fsck my ext4 filesystems, and I'm honestly not sure I've ever seen any unrecoverable corruption on ext4 or ext3. (I'm not saying it doesn't happen, as it certainly can, but I wouldn't think it's that common unless you're operating on a fairly large scale. And before journaling filesystems, crashes were of course a whole different deal.)

I used to keep /home separate on my personal devices (and maybe some other partitions as well because it was "a good practice"), but for more than a decade now my personal devices have had everything on a single large root fs, save for /boot and swap.

I've stuck with rather conservative fs choices and haven't used btrfs much, so I don't know if the story is different there.

What you gain with a single large root partition is of course that you don't need to know in advance how much software you'll want to install under /usr or how much space you'll need for /home or /var.

Multiple partitions can certainly be a good idea from a system management point of view, for cases where people either want to actively manage the system, or when their needs are demanding enough that it's warranted. If I were deploying a server for any kind of real use, yeah, the database or the server logs would certainly not be on the root volume.

But in case of desktops or laptops, most people, even engineers, don't really want to be meticulously managing their personal computers. Nor are most people's personal computing needs that demanding. The convenience of not running out of space on a single partition because you guessed the your future needs wrong wins over.

> Swap on a different physical drive is faster.

> Spread the load across multiple media for performance. That is how server OSes do it, because it is faster.

Of course it is, but in contrast, laptops don't really tend to have separate physical drives on which to put swap. I doubt most people would care enough about swap performance to install a separate high-performance drive for it even in desktops.

With that said, I also do keep a swap partition even on personal computers, and hibernation can be useful. (I think there are some issues with hibernation and encrypted swap in Linux, though, so I haven't bothered with it in a while).

Re: Stop making swap partitions—use swap files instead

#242

Or be a proper computer user and have a full drive on its own dedicated bus for swap. There is literally no point in time where a full swap drive is less performant or useful than swap directories or swap files (and swap files are a relic of the 90s when a single application would need gobs of virtual memory.) Funny watching someone advocate going back to something that sucked when we have far more performant hardwar…

I’m not advocating for anything but for distros to stop recommending swap partitions. I advocate for right-sizing your hardware to predicable workloads and not using swap at all. On your pet servers or desktops where the workload is experimental or wildly unpredictable, swap files make more sense over partitions.

"I advocate for right-sizing your hardware to predicable workloads and not using swap at all."

You do realize in the current semiconductor-short code-bloat-heavy economy we're in right now that your advocation would be prohibitively expensive for many people, yes?

At least, the gaming community would basically need to be grabbing $5K systems just because of garbage AI companies and crappy inefficient 'AI' upscaling algorithms with garbage engines that insist on doing intensive rendering optimization and upscaling on low-res models when simply rendering the regular model is faster (I'm looking square at you, UE5.) You know, that kind of thing. Plus the open world streaming, yea that's about already equal to a massive swapfile just for world data.

I think your advocation is very short on vision given current realities.

Re: Stop making swap partitions—use swap files instead

#243
post #240
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]

Nah, zswap is way better than zram: https://chrisdown.name/2026/03/24/zswap-vs-zram-when-to-use-... >.

zswap is back in kernel?

Edit: seems I got confused there, only frontswap layer was removed, and zcache

Re: Stop making swap partitions—use swap files instead

#244

Earlier quoted context omitted.

> If you have a small root partition, the default config Of what distro? If you mean that the example code shows the swapfile directly under / , well of course you don't have to do it that way. > With a partition, it's easier to put swap on a separate physical device, which is good for performance. I think this is untrue on both counts. First, nobody is going to dedicate an entire physical device to swap, because the…

I think you're wrong on every single point you make. > Of what distro? Of any distro. I have done such configs with Ubuntu, Debian, openSUSE, SLE, Fedora, CentOS, RHEL, Alpine, Arch, you name it. > If you mean that the example code shows the swapfile directly under / , well of course you don't have to do it that way. Point missed. If you have a swap file created by the installer then you do not get to specify where t…

> If you have a swap file created by the installer then you do not get to specify

Why should we care about what the installer does when it's trivial to change later? You said this is "bad advice, except for very simplistic scenarios" but people not in "very simplistic scenarios" are perfectly capable of running a few shell commands.

Aside from which, typical home users are in very simplistic scenarios. The Mint installer defaults to a single partition aside from what's required for booting, and a swapfile at /swapfile. And tons of Mint users are doing just fine with that. They are not realistically going to distro-hop because it won't realistically solve any problems for them.

Your overall argument seems to depend on accommodating some hypothetical power users who nevertheless want the installer to get it right for them. But in this case, why not petition the distro to improve the installer instead?

> This has been standard practice since the 1960s!

And? the point here is to identify a benefit of putting swap on a carefully chosen partition that cannot be realized by putting swap on a file at a carefully chosen path (since that choice also implicitly puts it on a specific partition).

> You can but suddenly permissions and ownership become issues.

I can't fathom why. I said /home, not ~.

> Point missed. ... Incorrect assumption. ... Have you read the other comments?

I consider this sort of commentary incivil (and the commenting guidelines support me in this).

I also did not make an assumption at all; I considered one of two alternative meanings without introducing it with the word "if". That's why the second possibility does start with "if". This is just a rhetorical technique, dismissing the most absurd interpretations first exactly because they are easier to dismiss.

Re: Stop making swap partitions—use swap files instead

#245

Earlier quoted context omitted.

> If you have a small root partition, the default config Of what distro? If you mean that the example code shows the swapfile directly under / , well of course you don't have to do it that way. > With a partition, it's easier to put swap on a separate physical device, which is good for performance. I think this is untrue on both counts. First, nobody is going to dedicate an entire physical device to swap, because the…

GP is not presenting these as evidence for an approach, just notable facts that weren't initially obvious.

The comment concludes with "Conclusion: no, bad advice, except for very simplistic scenarios.", so I don't think I can agree with you on this. My point was that these facts don't (in my analysis) support the conclusion, and suggest a contradictory stance (in particular: planning in advance to dual-boot multiple separate distros, but not being willing to adjust the configuration after installation).

Re: Stop making swap partitions—use swap files instead

#246

Earlier quoted context omitted.

As @mitxela says, if you need to fault in a page which is needed for some critical OS function, which requires going through the VFS layer, and that requires paging in another page, which has been swapped out due to memory pressure ... There are many edge cases. Swap on primary partitions also complicates dump and restore, although hardly anyone seems to know what backups are now.

See, the thing is that swapping or paging to a file does not use the VFS layer, except when activating or deactivating it. So swapfiles may not be materially different from swap partitions during operations. https://utcc.utoronto.ca/~cks/space/blog/linux/HowSwapFindsB... And there are many things that complicate dump and restore. Dump and restore often need to account for what partitions were created, anyway, so don'…

The swap files do use a non+VFS path after setup in 2.6+ kernels, but it is complex and has had bugs:

https://lkml.iu.edu/hypermail/linux/kernel/2103.0/06524.html

The bug is in here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/lin...

This was 2021, and while this bug is fixed, maybe take Linus's advice. Rely on a partition for primary swap, and only add files if you need to. Don't put them on the root partition, preferably a second disk.

I can't resolve your links but this is a good reference: https://kernel-internals.org/mm/swapping/

Normally dump skips swap partitions entirely, and doesn't know anything about partitioning. Swap will refuse to activate on a non-mkswap partition so there is reasonable protection when you're migrating.

Re: Stop making swap partitions—use swap files instead

#247

Earlier quoted context omitted.

As @mitxela says, if you need to fault in a page which is needed for some critical OS function, which requires going through the VFS layer, and that requires paging in another page, which has been swapped out due to memory pressure ... There are many edge cases. Swap on primary partitions also complicates dump and restore, although hardly anyone seems to know what backups are now.

See, the thing is that swapping or paging to a file does not use the VFS layer, except when activating or deactivating it. So swapfiles may not be materially different from swap partitions during operations. https://utcc.utoronto.ca/~cks/space/blog/linux/HowSwapFindsB... And there are many things that complicate dump and restore. Dump and restore often need to account for what partitions were created, anyway, so don'…

BTW you can swap over NFS and iSCSI with swap files, with NFSoIB, or use the Infiniswap modules:

https://github.com/SymbioticLab/Infiniswap

Great hack for when you have a massive working set but don't want to rewrite the code to be distributed/parallel.

Re: Stop making swap partitions—use swap files instead

#248
post #116

Earlier quoted context omitted.

the system will restart sshd?

the system will kill whatever it damn well pleases. You can tune it with priorities to ask it to try to not kill that, and once it kills your sshd once, you'll probably configure it to exempt sshd from being OOM killed at all. That doesn't fix the memory pressure or hanging or stalling, but you'll at least be able to log into the box still instead of dragging out a serial cable.

are you using systemd or some other init ?

Re: Stop making swap partitions—use swap files instead

#249
post #189

Earlier quoted context omitted.

If I recall correctly, Oracle best practices back in the day (waaaay back) was always put the DB on raw partitions, which caused us some issues with existing tooling. I've been fortunate enough to not have to pay attention to Oracle best practices in quite a while.

High performance databases still use raw I/O. You get much better control of data placement and write ordering, without filesystem confounders. With traditional spinning rust being replaced with SSDs and SMR it can be even more important. You may even have different firmware on the disks / disk controllers for database disks.

Sure...pretty clear there's a use case. I just recall our ops folks having to retool some of their playbooks because of the assumption there would be a standard filesystem on every active slice.

Fortunately, the whole "load a particular firmware on the disks" wasn't new. That was something some of the HW RAID vendors recommended.

Re: Stop making swap partitions—use swap files instead

#250

Earlier quoted context omitted.

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…

In the distant past there was plenty of times I tried to fix dying web servers by logging in and restarting a rogue process or cleaning up a memory leak... only to have my SSH session and shell sessions being repeatedly killed by OOMs. Not as big of a problem as it used to be. Both because OOM killer has gotten better and also web servers are such a commodity that remotely cycling them isn't going to cause a outage a…

>only to have my SSH session and shell sessions being repeatedly killed by OOMs

The OOM killer looking at processes and seeing a giant one, using 90% of the physical memory and growing fast and deciding "Oh, that's ok because it is working hard, I'm going to go kill this little tiny thing that hasn't been used recently called sshd". Inoculating sshd against the oom killer is SOP now.

Post reply on HN