9 ms·
Ask HN: Do we still need SWAP partition?
- toast0 3y agoHaving 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)
- yodi 3y agoDepend 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
- fernvenue 3y agoWow, so if I understand correctly, does this actually lead to better performance?
- RetroTechie 3y agoHate to say it, but you're doing it wrong. Depending on budget (and how expensive you consider your / development time), just choose between: a) Use smaller ML models / tweak configuration such that it actually fits into 196 GB. That's already a ffing humongous amount of RAM! Or b) Bite the bullet, and add RAM to fit the ML model(s) you're using. Your workflow sounds like running Win95x on a 1 MB machine. Painful! (if it works at all). Just add the RAM, or slap DOS + Win 3.x back onto it.
- dveeden2 3y agoWith 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-numa-architecture/ 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.
- fernvenue 3y agoSounds interesting, could you please describe some details about "lvm/btrfs/zfs make that less of an issue"? Because I use btrfs almost everywhere, so I really wanna know more details about this, or could you please tell me where I can learn something about that?
- mixmastamyk 3y agoBelieve the point is that resizing partitions is easier with those FSs. But not a big deal since Partition Magic.
- rleigh 3y agoOn ZFS, use a zvol as a swap partition. Easy to create, destroy, resize, have redundant copies for robustness, all of the configuration options you have for a data volume but use for swap. I've done this on both FreeBSD and Linux, never had a problem. However, IIRC there might have been some issues in the past on Linux.
- bhaney 3y agoI never use a swap partition anymore at all. I just create or destroy swapfiles dynamically as I feel I need them.
- dusted 3y agoI 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.
- fernvenue 3y agoI have similar memory space, thanks for letting me know these.
- mixmastamyk 3y agoNeed? 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.
- fernvenue 3y ago> I write text files for a living and its been plenty. I thought write text files may need swap partition more than others?
- mixmastamyk 3y agoWhy? My text files are typically under 100k and a good editor will pull larger ones in as needed. 0% of them are larger than RAM, ~16 GB.
- RetroTechie 3y agoSame here. I've been using a desktop with 4GB RAM (considered low-end these days, many phones have that), but no swap. Doing so for years. No big issues other than the occasional website (scripts, use of top-heavy web frameworks or whatever) causing runaway RAM usage. Local apps (including some older games, electronics design stuff) run fine. Most sensible argument for using swap I've seen is a memory leak on server hardware, where having swap keeps the server at least running, buying the admin time to figure out what's going on. On my PC, if an app leaks memory, I want to see free RAM going down to the point where something will crash - asap (no swap). Giving me a good sense which app (or website) was the culprit. Not (with swap), the whole system getting slower & slower, but with a delayed fuse & much fuzzier clues about which app caused the problem. Pick workflow + OS + apps such that working set fits RAM in the machine, and be happy. If needed, change OS / apps / workflow, or add more RAM. Not: take app that needs 16 GB to run acceptable, run it on 4 GB machine, and use swap to somehow make it work while the user experience makes you cry. So, imho: no need for swap on modern machines. Unless (maybe!) it concerns an internet facing server & uptime is critical.
- DamonHD 3y agoI run my RPI servers without a swap partition (but with a small amount of zram) to avoid horrible wear on the SD cards.
- fernvenue 3y agoWow, I also have Raspberry Pi, but I never thought like that, good point.
- Am4TIfIsER0ppos 3y agoDo 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.
- ac2u 3y agoIs hibernation typically done through the swap partition? I thought most OSs would just be using specialised sleep state files given that the hibernation image would need to be a serialised dump of all allocated memory whether resident on physical or paged out to the swap space. By extension, with swap disabled hibernation could still happen as long as there's enough physical memory free to handle writing to the sleep image on disk.
- sdfzguf 3y agoI'm a very beginner in my self learning journey. My understanding is, that hibernation is the same as 'suspend to disk'. If that is true, memory is cleared and put in a memory on disk (eg swap). From what I found on the Arch wiki, it works with a partition and file. Swap space can be used for [...] suspend-to-disk support. https://wiki.archlinux.org/title/Swap#Activation_by_systemd https://wiki.archlinux.org/title/Swap#Activation_by_systemd In order to use hibernation, you must create a swap partition or file [...]. https://wiki.archlinux.org/title/Power_management/Suspend_and_hibernate#Hibernation https://wiki.archlinux.org/title/Power_management/Suspend_an...
- ac2u 3y agoThanks for the links. I guess it makes sense to use swap space in aiding suspend to disk if some of the allocated memory is already paged out to it. I wonder though if in the [2] link you posted if you can have a swap partition for the purposes of hibernation, while also disabling swapping of memory while your machine is in-use.
- sdfzguf 3y ago
- countWSS 3y agoNO. 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".
- fernvenue 3y agoThanks! And you're absolutely right, actually my biggest concern when debating the need for SWAP partition is whether there are some programs that depend on the SWAP partition to work.
- speedgoose 3y agoI 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.
- fernvenue 3y agoInteresting! Maybe there are some differences between local devices and cloud servers.
- bjourne 3y agoNo, 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.
- fernvenue 3y ago> ...destabilizing the whole system by using the disk as ram. Could you please be more specific?