6 ms·
Stop making swap partitions—use swap files instead
- mdspan 26d agoSwap files are also much easier to set up than partitions if you're using full disk encryption.
- demomode 23d agoSwap files are very fragile if you are using hibernation. For FDE I prefer LVM-over-LUKS and a dedicate partition
- pasc1878 23d agoMacOS seems to have no problem with hibernating and it and its predecessor NeXT have been using swap files for over 30 years.
- killerstorm 23d agoMacOS has no problem with hibernation because hardware and software are designed together: it uses TPM to manage swap encryption. I assume "BIOS" is also optimized to enable fast boot on sleep. Linux can work with TPM but it's never as smooth. (Unless I guess you make it custom for your specific setup.)
- cedilla 23d agoI have FDE with keys in the TPM. It's pretty smooth bit required a one time additional step in Fedora. It's also probably not very secure, but my threat model is simple theft.
- avhception 23d agoWhat's the benefit of using the TPM compared to a simple passphrase at boot, in that scenario?
- theblazehen 23d agoA person wouldn't be able to pull the disk to get at the information
- avhception 19d agoAssuming that the person pulling the disk doesn't have the passphrase, what's the difference?
- theblazehen 17d agoAssuming things are configured correctly, the TPM would only provide the key to unlock the drive if the correct OS has been booted. Attempting to boot from a live usb to have a look at the data wouldn't work, since the TPM wouldn't present the key required. You can see https://wiki.archlinux.org/title/Trusted_Platform_Module#PCR_policies https://wiki.archlinux.org/title/Trusted_Platform_Module#PCR... for an example of doing so
- cedilla 18d agoSorry for the late answer: the benefit is that I don't have to enter my passphrase at boot.
- gertop 23d agoWindows has no problem with hibernation and FDE and it uses a swap file. Windows and Linux run on the exact same hardware. Windows hibernation doesn't rely on secret bios features. Unreliable hibernation is 100% and undeniably a Linux problem, there is no secret advantage or conspiracy to keep it down. It's just that nobody cares enough to make Linux not suck.
- jeroenhd 23d agoThese days with cryptenroll and friends, using the TPM is hardly an issue anymore. LVM and swapfiles both work well if you just create one large "everything but EFI" encrypted volume. You'd need to keep secure boot on, preferably with your own keys loaded, to get all of the benefits, and that's a bit harder to automate. Windows does all of this and more. It's harder on Linux because of a lack of tooling/interest/business incentives, and because on average Linux users want more control over their hardware than Windows users.
- fc417fc802 23d agoHow so? It's a one time setup procedure of only a few steps to create a tiny "protective" ext4 (or whatever) partition at the front. Thereafter you configure crypttab to mount the partition via UUID with an offset and key. If you don't need to hibernate you can read the key from /dev/urandom.
- somat 23d agoFor what it's worth(not much) On OpenBSD swap is always encrypted, With or without full-disk encryption. I don't know why linux always has to make the easy things hard.
- dwedge 23d agoInterestingly I always use swapfiles and this page made me realise there can be a performance impact if the filesystem is old - the lkml link says as long as the swap file isn't fragmented there's no impact
- WalterGR 23d agoIn practice, there’s a potentially tremendous impact if you’re using a spinning metal disk. Sectors at the beginning of the disk (which is at the outer edge) are read/written way faster than sectors at the end. File systems tend to prefer writing files at the beginning. If the file system has been used for ‘a long time’ then it’s either fragmented - or it’s been defragmented and the free space is at the slow end of the drive. One nice thing about swap partitions is that they can be fixed in place even before OS installation. (Of course, if you’re using swap enough for this to be a huge factor, you probably have bigger problems. But I haven’t done the math.)
- gblargg 23d agoIndeed. I'd always put swap near the beginning, then a small root partition, then the rest. This reduced head movement for often-accessed data, and used the higher bandwidth of the outer tracks.
- charcircuit 23d agoLinux distros that do use swap files do so statically. It doesn't handle dynamically growing and shrinking it like other operating systems.
- 0xbadcafebee 23d agoUnless you use the Linux package that does manage swap files dynamically.
- kaelwd 23d agohttps://github.com/Tookmund/Swapspace https://github.com/Tookmund/Swapspace
- deleted 23d ago[deleted]
- weinzierl 23d agoThere used to be a time when not only the OS required using partitions for optimum performace (swapfiles) but also applications. In the late 90s databases were regularly set up in a way to store their data on raw partitions. There were other types of applications too that required partitions but for databases it was really common. The practice really only died with OSes allowing apps to bypass the normal filesystem cache. In Linux this is O_DIRECT and interestingly Linux was super late to the party because Linus hated it with a passion. Now, he was far from the only one, at least Alan Cox and Andrew Morton were skeptical, others as well. Linus was unusually extreme about it and that particular discussion is probably the single source of the majority of quotes people use against him.
- RiverCrochet 23d agowas this before mmap(2)?
- topspin 23d agommap(2) in Unix predates Linux itself by several years (1988 Sun, 1990 BSD). mmap(2) was present in Linux well before any of the high performance databases, file systems and other applications that use(d) O_DIRECT appeared on Linux. O_DIRECT was resisted by Torvalds and others, but it's there today, and used by supposedly important platforms. Nvidia even has an API to DMA data to and from GPUs that uses O_DIRECT. There are folks working on new async WAL O_DIRECT for PostgreSQL as well. So what can be said? mmap(2) isn't sufficient for certain cases. Torvalds has faced and successfully navigated difficult design choices in a pragmatic manner. What else? The lesson I take is this: there are levels to implementation complexity and value. The wisdom to decide the appropriate abstraction level is crucial. You are unlikely to be equipped well enough to beneficially utilize O_DIRECT, but it's not impossible and some, in fact, are so equipped.
- klodolph 23d agoWell-put
- 22d ago
- holowoodman 23d agoStop using partitions. Use LVM!
- jenders 23d agoHow often are you needing to change partition geometry after install, on a server?
- holowoodman 23d agoDepends. Some customers do run a lot of "pets, not cattle" servers, and those can get a few extra hard disks if space runs out. Some security guidelines prescribe separate volumes/filesystems for e.g. /home or /var/log, so log spam doesn't kill the whole system as easily. But of course, this means that sometimes the initial sizes aren't right and you need to extend something. In those cases, which happen a few times per year, not using LVM would be a huge problem. But even for the "frequently reinstalled cattle"-usecase, LVM has benefits, e.g. snapshots for backups, debugging and forensics.
- simoncion 23d agoMy short answer is "Not frequently, but when I do, I'd be very angry about having to screw around repartitioning a disk instead of fiddling with an LV, VG, or some PVs.". I use LVM on all my non-appliance Linux systems, [0] both servers and desktops. Given that few (no?) filesystems support online shrinking but every one that you'd use as '/' supports online growing, my strategy is to take a fairly conservative guess at how big my various subdirectories need to be and leave most of my available space unallocated. Later on, if I -say- find that I need more space in '/' or '/home', or prevent a blowup in '/var/log' from eating up all the space in '/' by putting that on a separate LV, etc, etc, it's just a few commands [1] and the job's done. [0] ...someone else has configured the storage scheme for the appliances, and I'm not going to override their choices... [1] ...and some data copying in the case of backing a subdirectory with a new volume...
- Kim_Bruning 23d agoStop using LVM. Use ZFS! (ZFS might be described as the love child of lvm and btrfs, 'cept ZFS got there first) ( Very briefly: you hand ZFS a stack of disks (in mirrors or raidz groups, for redundancy) and it makes a zpool. Then you carve the pool into datasets as needed. Each dataset looks like an ordinary directory and takes only the space its files actually use, so you never guess partition sizes again. And each one can have its own compression, snapshot schedule, quota, encryption, and send/receive backups, etc etc etc. )
- Kim_Bruning 23d agoEvery rule has an exception. Beware of this advice on zfs, for instance. Meanwhile zswap (of no relation with zfs) is free performance. Is it a rule that everything starting with a 'z' must be cool?
- deleted 23d ago[deleted]
- optionalsquid 23d agoIt also doesn't sound like a particularly good idea on BTRFS, judging by the linked documentation: https://btrfs.readthedocs.io/en/latest/Swapfile.html https://btrfs.readthedocs.io/en/latest/Swapfile.html
- fc417fc802 23d agoAnd since you should probably be running a modern CoW filesystem then you probably shouldn't be using swapfiles ... is the conclusion I arrived at anyway. Either install multiple times more RAM than you need and enable zswap or else set aside a swap partition as has always been done.
- jenders 23d agoTruth be told—- I don’t use swap and set `vm.swappiness=0` but in these difficult times, swap may be the hero we need.
- jeroenhd 23d agoSwapfiles 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.
- cesarb 22d ago
- ducktective 23d agoTangential, 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?
- elektrontamer 23d agoI always install earlyoom for that reason
- dev_hugepages 23d agoThis 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.
- ButlerianJihad 23d agoI believe that you mean: https://en.wikipedia.org/wiki/Thrashing_(computer_science) https://en.wikipedia.org/wiki/Thrashing_(computer_science) Chris Siebenmann discusses when the OOM killer triggers: https://utcc.utoronto.ca/~cks/space/blog/linux/OOMKillerWhen https://utcc.utoronto.ca/~cks/space/blog/linux/OOMKillerWhen Chris disables systemd-oomd after it obliterates his X session with no explanation: https://utcc.utoronto.ca/~cks/space/blog/linux/SystemdOomdNowDisabled https://utcc.utoronto.ca/~cks/space/blog/linux/SystemdOomdNo...
- rovr138 23d ago> First off, this is exactly how systemd-oomd is supposed to behave under memory pressure. The documentation is specific on this; systemd-oomd itself says: > > [...] If the configured limits are exceeded, systemd-oomd will select a cgroup to terminate, and send SIGKILL to all processes in it. [...] > By having the user@.service template be enrolled in systemd-oomd, Fedora made the cgroup that systemd-oomd would select to be killed be all of your processes (across all of your sessions, if you have more than one). ... Maybe *Fedora* has fixed or improved in the last 4 years. Or maybe they don't run Fedora.
- cfstras 23d agoswap files make setting up hibernation a bit more complicated - in most cases the file has to be contiguous, e.g. you have to defragment it once after allocating. Then, you need to tell the bootloader the byte offset of the file on the partition.
- jenders 23d agoIf you want to share step-by-step, I’ll update the gist.
- cfstras 23d agoThat’s my point, it’s different for each distro & partitioning setup, so I wouldn’t know what to share here. Having a swap partition makes it as easy as adding `resume=/dev/sdXYZ` or `resume=UUID=…` to your kernel parameters.
- simoncion 23d agoAnd using LVM for "partitioning" bypasses a lot of this silliness. Do 'lvcreate --contiguous' and you should be good to go if you're using most any non-toy initrd. Given that every r/w filesystem you're going to use as your '/' on a Linux desktop or server supports online growing (and most support offline shrinking), it's nuts to me to hear people worrying about getting partition sizes correct. With LVM, if you get it wrong you just resize the LV and the filesystem it contains. Hell, LVM makes swapping out disks a really easy online operation. [0] If you don't have nice hot-swap drive caddies, you might have to power off the box once to add in the new disks and again to remove the old ones, but all the data transfer can happen while the system is in use. [0] Assuming the new and old disks have the same logical sector size, which they usually do.
- MBCook 23d agoDoes contiguousness matter these days? Especially since all disk locations are lies anyway as SSD firmware has its own layer of indirection too?
- mcculley 23d agoIt would be nice if the VFS had a hook for providing swap pages, making this manual futzing less necessary.
- crabbone 23d agoOh, the irony of: echo "/swapfile none swap defaults 0 0" >> /etc/fstab If we are going full modern, why not create a Systemd unit? ( https://www.freedesktop.org/software/systemd/man/latest/systemd.swap.html https://www.freedesktop.org/software/systemd/man/latest/syst... ) if you need more info. If you didn't know this: /etc/fstab is not used directly after the pivot. Systemd parses it, creates units for every record in it, and that's what gets executed in reality. I can't imagine why would you want a swap file before pivot.
- jenders 23d ago[flagged]
- SahAssar 23d agoIf you are going to make recommendations and promote them to a wide community (linux users) that is predominantly using something that you do not consider advisable for that recommendation (systemd) then it might be good to mention that. Systemd is probably on 80%+ linux systems that people interact with, and 95%+ that people manually configure.
- delta_p_delta_x 22d agoThis single comment of yours has in my eyes made your entire post worthless. SystemD has single-handedly consolidated and streamlined Linux administration. I think valid criticism is useful, but calling it malware is a non-sequitur.
- waynesonfire 23d agoIt's a terrible idea to delegate swap, a capability that is expected to operate under severe memory pressure, to a subsystem that fails under such conditions.
- jenders 23d agoWhat subsystem are you referring to?
- williebeek 23d agoRemember when distros used complicated partition setups, one for /root, one for /var one for /home, a swap partition etc. Was always a bad choice because one of them would be at 99% while others would linger below 10% For swap, the best advice is to disable swap on your desktop, unless <8GB RAM. Really, I've never needed it and you probably won't either.
- jenders 23d agoAgreed. Buy more RAM but these days that means taking a HELOC
- tacomagick 23d agoId always suggest at least 2-4G of swap even if you have plenty of memory 32g+ because for that rare edge case where you'd really not want to OOM.
- ChocolateGod 23d agoYep. Swap + zswap + earlyoom is a great safety need incase things get hairy.
- blablabla123 23d agoOn the Desktop fine-grained partitioning is a bit questionable but on a server it's still quite practical. E.g. preventing the database or excessive logs from filling the disk space. The latter is also an attack vector actually. Also generally swap is recommended on all systems. During normal use it should rarely be needed but if it's needed it prevents spurious crashes. E.g. make -j16 shouldn't result in a hard to comprehend crash because I miss 1 GB of RAM.
- crabbone 23d agoI'm not sure if that's a bad choice... BTW, I think STIG still requires it. So, if you want to be compliant with some (US) government requirements, you'd still have to put different bits of your system on different partitions / devices. Why I'm not sure this is a bad idea: a pathological process that quickly captures the entire allocated disk space will be contained by this layout and will likely not affect the system as a whole. Consider, for example, a process that excessively logs into /var/log: once the filesystem mounted at /var fills up, it might fail / hang, but you will still be able to use stuff from /bin or /sbin. But, if your entire root filesystem fills up, you probably won't be able to run any applications at all.
- kiln_ash 23d agosystemd-oomd killing your whole session because Fedora enrolled user@.service is nasty. A swap file won't save you from the OOM killer, just delays it.
- cryptonector 23d agoBut first it makes your system slow.
- spijdar 23d agoWhy use swap files when we could have swap directories? ;-) Sprite had (IMO) a really interesting solution to swap. Each host had a directory, referenced by its host ID under the `/swap` directory, and inside that directory, individual segments of virtual memory would be saved: https://github.com/OSPreservProject/sprite/blob/master/src/kernel/vm/vmSwapDir.c https://github.com/OSPreservProject/sprite/blob/master/src/k... Sprite overcommited memory like a modern OS, and stored segments individually so that process migration worked (if a segment was swapped-out, the path to the swap file would remain the same anywhere on the cluster, so if the process was migrated, you didn't need to swap-in the segment before migration): eery@cherimoya [1] > cd /swap eery@cherimoya [2] > ls 1 10 11 12 13 14 15 16 17 2 3 4 5 6 7 8 9 eery@cherimoya [3] > ls 3 1 113 129 148 161 172 185 20 218 238 252 33 48 63 77 95 100 114 130 15 162 173 186 203 219 24 253 34 49 64 8 96 101 115 132 150 163 174 187 204 22 240 254 36 50 66 80 97 102 116 133 152 164 175 188 205 221 242 255 38 51 67 81 98 103 117 134 153 165 176 19 207 222 243 26 39 52 7 82 99 104 12 135 154 166 177 190 209 225 244 27 4 55 70 85 105 121 139 155 167 179 191 21 226 246 28 40 56 71 86 106 122 14 156 168 180 193 210 227 247 29 42 57 72 87 107 125 140 157 169 181 194 213 23 248 3 44 6 73 9 11 126 143 158 17 182 197 214 230 249 30 45 60 74 90 111 127 145 159 170 183 198 215 233 25 31 46 61 75 91 112 128 146 16 171 184 199 216 237 250 32 47 62 76 92 eery@cherimoya [4] > ls 5 82 83 eery@cherimoya [5] > ls 15 111 126 14 155 17 181 217 253 4 57 72 86 98 116 131 141 160 170 19 24 29 47 62 77 9 121 136 150 165 177 210 25 34 52 67 8 95 eery@cherimoya [6] > grep cherimoya /etc/spritehosts 12 pc386 cherimoya.shockfox.net cherimoya eery@cherimoya [7] > ls 12 eery@cherimoya [8] >
- genxy 23d agoAre you running Sprite in qemu or on hardware? I ran a small Mosix cluster a couple times just for funsies. I had forgotten about Sprite as the other SSI cluster operating system. It is really too bad that this line of research trailed off.
- 23d ago
- Toynbeeidea 23d agoNo.
- kazinator 23d agoSwap partitions are your set-and-forget permanent installation that covers daily use. Swap files are for "oh shit, I need to run this rare thing that needs lots of VM". You create them, "swapon", then "swapoff" and remove when done.
- deleted 23d ago[deleted]
- nicman23 23d agoi cannot hibernate in a swapfile in zfs
- monster_truck 23d agoYeah, no. I don't care if I have 128gb or 2tb of ram, or what I'm doing with it (but especially in live/realtime scenarios), I'm always going to have at least one nvme dedicated entirely to swap.
- rini17 23d agoUse 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]
- kees99 23d agozram 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.
- fpoling 23d agoZram 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.
- MrDrMcCoy 23d agoZram support for a block device is quite misleading, since there are no automatic mechanisms for enabling this. You have to build your own daemon to scan pages and take action on them. Zswap doesn't have that problem.
- fpoling 23d agoOn 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.
- Rohansi 23d agoIf you care about performance you shouldn't be running from swap period. Swap is just a safety net.
- SoftTalker 23d agoFor 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.
- malwrar 23d agoAny tips on measuring the impact of not having swap? I never use it on my machines for similar reasons.
- ktm5j 23d agoWorst thing that's ever happened to me is having processes randomly killed by the OOM killer. Could be unpleasant if you're writing an email or document that suddenly disappears because your app got killed.
- mitxela 22d agoThe OOM killer usually kills the process that's using the most memory, which is the reason the system is OOM. Sometimes this can be adjusted. When I'm OOM it's almost always because some runaway loop used 50GB and it makes no sense to kill any other process. If it's killing your email editor then your email editor is bloated or you just need more RAM - or swap.
- ktm5j 20d agoWeb browsers are most people's email editor.. and yes those things do use a lot of RAM.
- anyfoo 23d agoIt highly depends on your specific workload, but freeing up RAM by paging out rarely or downright unused pages to swap can definitely give a performance advantage. In consequence, I always give swap. Even on the off-chance it ends up unused, disk space is orders of magnitude cheaper than RAM, so I don't mind sacrificing it for potential gains.
- mindwok 23d agoTypically I see admins create partitions as basically failure domains - if one fills up it doesn't explode things relying on the others. Wouldn't this be worse in that regard? If you end up accidentally filling up your entire disk, you can no longer swap. Vice versa, if you swap aggressively you could end up filling the disk and then break a running process.
- koala_man 23d ago> Swap files have had the same performance characteristics as swap partitions for more than 20 years I thought so too, then I benchmarked it on a (spinning rust) HDD and found it was significantly slower: https://www.vidarholen.net/contents/blog/?p=1110 https://www.vidarholen.net/contents/blog/?p=1110 tl;dr: "In this test, using a swap file was surprisingly 50%+ slower than simply allocating a swap partition at the start of the drive, in spite of the low fragmentation and Linux’s bypass of the FS layer."
- Aurornis 23d ago> hen I benchmarked it on a (spinning rust) HDD Yes, but as your article notes HDDs have different performance characteristics depending on where the sectors are physically located on the platter. If you could force the swapfile to the same location on the platter it shouldn't perform differently. Most people are going to be using solid state where this doesn't matter.
- deleted 23d ago[deleted]
- jenders 23d ago+1. This test and its conclusions are flawed. See CAV/CLV
- cesarb 23d ago> 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.
- jwilk 23d agoAlso, fallocate+chmod is racy: between the two calls, an attacker could open the file, and then keep it open until next reboot. OTOH, mkswap creates the swap file with the correct permissions straight away. The --size and --file options are relatively new, though: they were added in util-linux v2.40, released in 2024.
- zahlman 23d ago> between the two calls, an attacker could open the file, and then keep it open until next reboot. I feel like if there's a malicious user (or program) on your system with the necessary access for this, you have much bigger problems.
- jenders 23d agoI wrote this years ago before LLMs made it easy to validate guesswork. I’ll update! Good call
- bigstrat2003 22d agoLLMs are guesswork. You have to validate them, not the other way around.
- jenders 23d agoLooks like this was added in util-linux 2.40. Works with btrfs correctly too! Added.
- lightedman 23d agoOr 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 hardware today which makes most of the other stuff wholly irrelevant.
- jenders 23d agoI’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.
- lightedman 22d ago"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.
- tarruda 23d agoThe exception is if you use CoW filesystems. Btrfs has a workaround: it can create a cow-disabled subvolume for the swapfile. AFAIK you need a separate partition for Zfs.
- ghostly_s 23d ago[flagged]
- jenders 23d agoAh the old HN hug. I’ll fix it
- copperx 23d agoWhat bug?
- zahlman 23d ago"Hug", not bug. Referring to a site (presumably under OP's control) crashing due to increased traffic from HN.
- wccrawford 23d agoThe hug of death.
- embedding-shape 23d agoAlso, this seems to be based on a email from 2005, about kernel version 2.4 and 2.6, and their differences. Things might have changed since then, and also depends on a lot of factors, not the least what filesystem you use. I'm not sure people should seriously follow advice that basically boils down to "do this always it's best". Better instead to make yourself informed about the tradeoffs, then make your own choice, for your setup.
- thyrsus 23d agoMost file systems get cached in ram, which is unlikely to have happy consequences when scarce ram triggers use of the filesystem swap.
- sehw 23d agono u
- lloydatkinson 23d agoI've always thought it a bizarre and unusual "requirement" that swap gets an entire partition. Windows has it as a file too. At least with this approach, if you change the amount of RAM in your computer, you don't need to go resize the swap partition if you want them to match.
- dogline 23d agoWell, this post made me reassess using zswap vs zram. I've expanded my zswap and disabled zram. Not sure if that makes any difference to others, but it's probably good for me.
- lproven 23d agoCouple 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. This means it's more space efficient and keeps down partitioning complexity. * And of course there's hibernation, as others have mentioned. Conclusion: no, bad advice, except for very simplistic scenarios.
- zahlman 23d ago> 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 smallest storage devices you can get are enormous compared to the RAM they're trying to support. If the device is "separate" (from what, / ?) but still has other things on it, it's even less clear why this would matter for performance. But also it's trivial to put a swapfile on whatever device you want. It can be anywhere reasonably sane in the filesystem, you just give it a path. > you can share a single swap partition between distros no problem The file could go on whatever partition the distros can share. Maybe put it under /home or something. It's your system, the FHS is up to your own interpretation. > And of course there's hibernation, as others have mentioned. Why would that matter? You're expecting a file not to survive the low-power state?
- dwattttt 23d agoGP is not presenting these as evidence for an approach, just notable facts that weren't initially obvious.
- zahlman 22d agoThe 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).
- Sohcahtoa82 23d agoAre there people still using swap partitions? I've been using swap files on Linux for years now.
- jenders 23d agoyou love to see it
- PunchyHamster 23d agoI love how there is no explanation why, or any info why wasting 2GB as partition is any different than as file
- jenders 23d agoThis is a mechanism post not a policy post. If you want to see my take on swap, see my last response on that gift.
- HackerThemAll 23d agoWhy do I still need swap space at all, be it a swap file or swap partition? I have 64GB of RAM and I don't overprovision it. WHY DO I HAVE TO CONSIDER SWAP in 2026?
- jenders 23d agoSwap is not recommended on servers where responsiveness and determinism are more important than failure recovery of any one server.
- throwawaypath 23d agoSwap is disabled everywhere. Endpoint hardening.
- aidenn0 23d agoNot all filesystems support swap files.
- meitham 22d agoThis is true on ext4 but don’t use swapfile with ZFS or btrfs fs.
- gbraad 22d ago.. and often the only solution for a VM/hosted machine. It gives you a flexibility you otherwise not have, expand/shrink when needed; a partition is static/fixed in place. Often used them, and still do; `dd of=/swapfile if/dev/zero bs=1M count=1024`/`mkswap`, and `swapon`.