Live data from Hacker News

Stop making swap partitions—use swap files instead

gist.github.com

231–240 of 256 posts

Re: Stop making swap partitions—use swap files instead

#231

Couple of things this didn't consider. * If you have a small root partition, the default config puts the swap file on there, wasting precious space. * With a partition, it's easier to put swap on a separate physical device, which is good for performance. * If you dual boot 2+ Linux distributions or installations on a single machine, as I do routinely, you can share a single swap partition between distros no problem.…

It also clouds disk usage stats, as a big chunk of your root device is taken up by the swap file. I'd rather look at `df /` and see the size of my OS installation without anything else.

Partition resizing is not exactly difficult, and I can count the number of times I've ever actually had to resize a swap partition in the last 30 years on the fingers of one foot.

Re: Stop making swap partitions—use swap files instead

#232

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]

You’ve tainted your otherwise interesting comment pretty intensely by mixing in this level of condescension and hostility.

Re: Stop making swap partitions—use swap files instead

#233
post #212

Earlier quoted context omitted.

It's actually incredibly sensible not to just make changes to core OS settings unless you know what you're doing. There are good reasons not to use swap files, which become evident in OOM situations.

How does a swap file vs swap partition change things in OOM situations?

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.

Re: Stop making swap partitions—use swap files instead

#234
post #212

Earlier quoted context omitted.

How does a swap file vs swap partition change things in OOM situations?

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't swap partitions also complicate it?

https://utcc.utoronto.ca/~cks/space/blog/sysadmin/SwapfileAn...

Re: Stop making swap partitions—use swap files instead

#235
post #214

Earlier quoted context omitted.

I’m not sure I understand. In your equations, why is swap taking away from RAM? It’s purely additive. Say you have 64GB of RAM, of which 4GB are dirty pages that are never, or at least very rarely, accessed. With 64GB RAM + 4GB swap, you filled up your swap with them and have 64GB RAM left to use actively. With 64GB RAM + 0GB swap, you only have 60GB RAM left to use actively.

In my examples nothing is taking away from nothing, I'm just asking if, if two situations have the same total amount of GB, what's the actual difference between RAM+swap vs entirely RAM. Use the 32+32 numbers if you're more comfortable with those round numbers. If I have a system running comfortably with 32GB RAM + 32GB swap (no OOM, no swap thrashing), and I upgrade to 64GB RAM + 0 swap, what actual negative effects…

Adding 32GB of RAM is vastly better than adding 32GB of swap, it’s not even close. However, it’s also vastly more expensive. That has always been the case, not just in the current insane situation.

We use swap because we have plenty of disk space, but (comparably) very little DRAM.

The same goes for another level on the hierarchy: If you could choose between 1GB of DRAM vs. 1GB of SRAM (which your CPU cache is), you’d definitely choose the SRAM. But you’d be paying out of your nose both with money and area, it’s not economical at all. My CPU has 96MB of cache, and that’s considered massive.

Re: Stop making swap partitions—use swap files instead

#236
post #232

Earlier quoted context omitted.

[flagged]

You’ve tainted your otherwise interesting comment pretty intensely by mixing in this level of condescension and hostility.

I, uh, would be more careful of making that statement given our public comment history. I know I can't cast any stones here.

More than interesting though, I will vouch for many of the points they presented in practice. Spreading partitions and taking advantage of the flexibility of Linux with how and where they are mounted has been a part of best practices for decades now. Many cloud-based solutions for Linux workstations I've used take full advantage of this for easier management of space (i.e. volume expansion) as well as security considerations, to say nothing of keeping your /var/log separate to avoid running out of space because you turned on verbose mode for something.

Re: Stop making swap partitions—use swap files instead

#237
post #236
post #232

Earlier quoted context omitted.

You’ve tainted your otherwise interesting comment pretty intensely by mixing in this level of condescension and hostility.

I, uh, would be more careful of making that statement given our public comment history. I know I can't cast any stones here. More than interesting though, I will vouch for many of the points they presented in practice. Spreading partitions and taking advantage of the flexibility of Linux with how and where they are mounted has been a part of best practices for decades now. Many cloud-based solutions for Linux worksta…

I’m not sure what you’re trying to say.

I deserve to be called out just the same if I’m wrapping good points with “Drivel. Absolute piffle.” and “Maybe you are young and inexperienced”.

Re: Stop making swap partitions—use swap files instead

#238

Earlier quoted context omitted.

It also doesn't sound like a particularly good idea on BTRFS, judging by the linked documentation: https://btrfs.readthedocs.io/en/latest/Swapfile.html

Swapfiles on BTRFS seem to work great if you allocate them early (with the command linked in your article) and put them in a different subvolume than the rest of your system. BTRFS in general seems more I/O constrained than ext4 or xfs so it's probably still worse to put a swapfile on a BTRFS partition, but so far my systems are doing fine.

> BTRFS in general seems more I/O constrained than ext4 or xfs so it's probably still worse to put a swapfile on a BTRFS partition,

AFAIK, on Linux swap bypasses the filesystem. It asks the filesystem for the extents which are used by the swapfile, and does the I/O directly on the block device. This explains the many restrictions swapfiles have on btrfs (the file can't be copy-on-write, can't be mirrored, etc), but it also means the speed for a swapfile will be the same on btrfs as on ext4 or xfs (assuming a similar layout, that is, the file is not too fragmented; IIRC, the kernel prints the number of extents when you do a swapon, so you can see when it's too fragmented).

Re: Stop making swap partitions—use swap files instead

#239

Earlier quoted context omitted.

This is called an oomkiller. The kernel has one but it kicks in very late and the kernel prefers to do page trashing instead of killing processes. systemd-oomd should be integrated in systemd, you can configure it to your liking and see if it improves your problem.

I wish there was an easy way to configure it to say "target user processes first, specifically java (or these days python)" as in my experience they are always the culprits. Processes owned by system accounts or root should be the last ones killed.

systemd-oomd works reasonably well and there is source code. Perhaps claude can help add more detailed policy support to it.

Just found this comment:

https://news.ycombinator.com/item?id=49663299

Re: Stop making swap partitions—use swap files instead

#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-...>.
Post reply on HN