16 ms·
In defense of swap: common misconceptions (2018)
- jmillikin 5y agoI disagree with the position in this post -- in my experience swap has universally been a contributor to system instability and performance issues. I run all my Linux servers without swap, and for workstations try to restrict its use to suspend/hibernate only. 1. Allowing a process's mapped pages to be flushed to disk will cause performance to become unpredictable. This applies to both anonymous pages written to swap, and file-backed pages. The first thing every server process should do is mlockall(MCL_FUTURE) so the kernel can't decide to swap out parts of your RPC handlers. More sophisticated implementations (e.g. databases) can selectively mlock() specific pages. 2. Using swap to mitigate memory overcommit isn't useful because the process that overcommitted its memory should just be killed instead. This is where cgroups are useful: you can load test to understand the allocation curve, then tell the kernel to limit your service's process to 64 GiB or whatever. If it tries to go wild and take up more than its share, it gets a SIGKILL instead of taking the entire machine out of service. 3. Swap will destroy SSDs. You thought the write load from logging is bad? Try putting a consumer-grade SSD into a machine with 512 GiB RAM and let the kernel swap to it -- that SSD will be dead in a year.
- gjs278 5y ago
- linsomniac 5y agoMy reading of your comments above is that you are speaking about about cases where swap is being used as a replacement for memory. I take that from, among others, "Swap will destroy SSDs". Swap used only for paging unused data out is an infrequent occurrence, no way it's going to kill an SSD. My reading of this article is that he is specifically advocating NOT using it as a replacement for having adequate RAM. My experiences, around 5 years ago, of running ~100 VMs without swap, basically confirmed the assertions in the article. Mostly the systems would run ok, but when they got into memory pressure they would live-lock. I had hoped for just OOM killing, which happened sometimes, but also sometimes the machine was just locked. I switched over to having some small amount of swap, maybe 0.5-1GB on these boxes and haven't had a livelock since. Plus, it's better to get unused pages out of RAM, which can be written to swap. The one thing that HAS killed an SSD, in this case an Intel (320 series sticks in my head) is ZFS slog+L2ARC. Swap hasn't been a problem in my experience, but I also am not using swap to account for not having enough RAM.
- theamk 5y agoI've never seen those livelocks before -- but I saw plenty of swap thrashing. How much memory did those VMs have?
- linsomniac 5y agoI don't recall exactly, but probably 1-4GB. Not particularly low memory, but when something would come along that needed more memory than the system had, say a load burst, I was quite surprised to find the systems without swap just stop responding. I was expecting OOM killer to kill the offending process, the load balancer to remove it from the load, monitoring to say what process had gotten killed, but instead, even after waiting half an hour, the system was just wedged.
- theamk 5y agoThat seems pretty low, especially if this was 1GB of RAM. I'd use swap on such systems. I don't think there are any reasons today to buy physical machines with that little RAM, but I can see how you might have such VMs, That said, I agree with other commenters that said this sounds like a kernel bug. I am wondering what was going on to wedge the system that bad. Would putting executables on tmpfs (so they cannot be paged out) helped?
- bogomipz 5y agoIs "live lock" here page cache thrashing?
- tsimionescu 5y agoI think it's usually code page thrashing, since executables and shared libraries can always be swapped back out to disk, only to be swapped back in when the process gets the next quanta of time to execute. If people say instruction cache misses are costly, wait till you need to read the next instruction from a 5400 RPM HDD...
- 5y ago
- codeflo 5y ago> Try putting a consumer-grade SSD into a machine with 512 GiB RAM and let the kernel swap to it -- that SSD will be dead in a year. Are you sure? I’ve heard the argument before that swap is one of the better cases for SSD longevity, because it’s few big writes instead of many tiny ones, and found that convincing. Also, on which workloads would a machine with that much RAM touch swap at all? My workstation has “only” 64 GiB, has swap configured (to enable suspend), even during heavy use with some databases running never touches it.
- marcosdumay 5y agoThe alternative of the large writes swap does is not a lot of smaller writes, it's no write at all. Also: > on which workloads would a machine with that much RAM touch swap at all On most of them? You seem to be using a flawed model of the Linux swapping algorithm. Writes aren't caused by lack of memory.
- codeflo 5y agoI know you're not the GP, but please give some specific examples of such workloads, I honestly want to know. To be clear, yes, it's well-known that the kernel will page out rarely used stuff, but those are (almost by definition) pages that are basically never written to. The claim was that writes to swap occur all the time, to the point where they wear out the SSD, that seems to be something different. At least I've never seen anything like that with any of my (workstation) workloads, like large C++ builds, TensorFlow experiments, or running lots of virtual machines. And that was only with 64 GiB RAM, not even the 512 GiB that the post I replied to mentioned.
- marcosdumay 5y agoThe kernel write the pages into swap long before it decides they are rarely used. Otherwise rewriting them would be way too slow to be of any use. Any normal use will fill some swap space, and if the memory pages keep getting dirty (and you have spare disk IO), your computer will keep writing them to swap. How much RAM you have is not really relevant.
- PetahNZ 5y agoSo I run servers used to process images, 99% of the time it is typical ~5 megapixel photos, but occasionally it will be ~150 megapixels. We use swap because that 99% of the time it will fit in 4GB of ram. The occasional ones take an understandably longer time to process.
- db48x 5y agoI had good success using an Intel Optane drive for swap. Of course, that was because the task I was doing required hundreds of gigs of memory and my poor little machine only had 64; not exactly common in a business setting. Performance was great. Normally when swapping the performance of the machine is terrible, as you say. But those Optanes are so fast that it just didn’t matter. I was doing several hundred thousand operations per second, and they were all taking <4µs. It was a wonderful upgrade.
- simoncion 5y ago> Swap will destroy SSDs. You thought the write load from logging is bad? Try putting a consumer-grade SSD into a machine with 512 GiB RAM and let the kernel swap to it -- that SSD will be dead in a year. I have an OCZ Vertex LE in a 32-bit laptop that was my daily driver for somewhat-serious development for the better part of a decade and sees somewhat regular use as a bedside media consumption machine. It ran (and still runs) Gentoo Linux, so -while it was my daily driver- its drive saw _frequent_ writes due to weekly system-updating-build activity. This machine has 4GiB of RAM installed, of which only 3.2 GiB is available due to weird BIOS limitations. It has _always_ had swap enabled, and has _often_ made heavy use of it. This SSD has 93,560 power-on hours, and has written 48,384 GiB. As far as I (and SMART) can tell, it's as good as the day I flashed the v1.1 firmware on it. I don't believe your claim is generally true. Other folks who have intentionally run drives _way_ past their advertised wear-out points have similar stories to mine.
- jmillikin 5y agoRunning a light workload on a consumer SSD is fine. That's what they're designed for. Your ~48 TiB of writes over a decade is well within its design parameters. If you used it as swap in a server, you could expect something closer to its max write rate, which is on the order of 10-20 TiB per day[1]. Run that for a year and you're at 3+ PiB total writes. [1] Contemporary reviews say the OCZ Vertex LE had a maximum write throughput of 250 MB/s. Times 3600 (per hour), times 24, converted to IEC notation is 20,116 GiB per day.
- Teletio 5y agoIf your swap is constantly used. Which you should avoid by having proper monitoring.
- zozbot234 5y ago> Swap will destroy SSDs. You thought the write load from logging is bad? Try putting a consumer-grade SSD into a machine with 512 GiB RAM and let the kernel swap to it -- that SSD will be dead in a year. You can run with a low swappiness value and the kernel will not proactively swap out process memory unless required. Even light use of swap is probably fine as it likely involves rare process pages that will never be written to after being swapped out so there's no threshing on the SSD, only a few large writes.
- WantonQuantum 5y agoAhh now I understand why people disable swap on Linux boxes. I came from a SunOS/Solaris & HP-UX background (late 90s) where we routinely configured swap for the machines. Later on I encountered Linux admins who refused to configure even a small amount of swap and they couldn't explain why. It seemed very odd to me. Nobody ever said the simple "swap sucks in the Linux kernel" to me.
- throw0101a 5y ago> I came from a SunOS/Solaris & HP-UX background (late 90s) where we routinely configured swap for the machines. Most non-Linux Unixes also do/did not allow memory (RAM+swap) over-commit by default, which may have coloured behaviours.
- linsomniac 5y agoIndeed. At a past job we had this million dollar cluster of HP machines (T520s), and one of the managers wanted to do some end of year spending, so they upgraded the memory in the cluster from 2GB to 4GB (back when that was an insane amount of memory, ~1995). But, we had not storage to back that new memory. They purchased 2GB per machine for god knows what, but didn't add 2GB discs. So we literally couldn't use it (because of how storage was allocated on that cluster, we couldn't just re-carve it to get an extra 2GB swap, it had dedicated 2GB drives for the swap).
- jstanley 5y agoWhy couldn't you use memory without corresponding swap space?
- linsomniac 5y agoThe HP-UX kernel required at least as much swap as RAM, due to the way it handled memory and swap. I used to know the details of those algorithms, but have freed those portions of my memory for other uses. It was something like: The algorithms HP-UX uses for virtual memory consider that memory based on swap, and the RAM is kind of like a cache layer in front of the swap. This makes the handling of allocation and swapping easier because you know where everything would go before you start trying to swap.
- rektide 5y agoA mini version of this was presented in the wonderful & amazing "Linux Memory Management At Scale" video, also by Chris Down: https://www.youtube.com/watch?v=cSJFLBJusVY&t=11m42s https://www.youtube.com/watch?v=cSJFLBJusVY&t=11m42s You should have swap. You should have swap!! This video is one of the most amazing, useful, gem-after-gem explanations of Linux you could possibly ever encounter. There are so so few who have the in depth knowledge, across so many broad experiences, at such scale, to understand Linux's memory management. Side topic #1: this video's advice on cgroups just makes me sad, because I love Kubernetes, but Kubernetes building it's own scheduler, in contest with not in tandem with the kernel's cgroup system, is super super super f-ing sad. Kubernetes running all it's shit in a single cgroup is some decrepit sad sad shit. I still love & user Kubernetes, but god damn, for what basically amounts to a cloud os, it's using the underlying host-OS extremely extremely poorly. This video was key in making me think & consider how much we've lost, how sad it is we don't take advantage of what the kernel is trying to do for us with trading off workloads. Leaving the kubernetes scheduler to try to fit all the workloads feels like we're letting a bunch of ignorant elementary schoolers run the show. It's embarrassingly superficial. Side topic #2: it would be really interesting to try to go a bit deeper on the topic. I'd love a world where we can easily ask- what are these anonymous rarely used memory pages we get to swap out? That's the main advantage, under low & medium pressures: the system has some flexibility to move out things not really used, where-as it has to keep everything resident without swap. But answering: what are these things not being used: that probably requires bptrace foo to figure out what regions are getting swapped, plus deep skills to figure out what those addresses correspond to. I doubt Linux will just tell us, oh, I evicted this text block from the binary. (It likely doesn't know much about what blocks it's evicting.)
- marcosdumay 5y agoKeep in mind that positive memory pressure is something that if done by more than a single application at a time, will completely break your system, and if done only one application at a time will severely reduce your performance predictability (but can indeed increase throughput). So, what he talks about on that video is a very specialized performance optimization that may work on some (certainly not all) systems that you have complete control of. It isn't general advice in any way.
- assbuttbuttass 5y agoAlthough the article claims "Disabling swap doesn't prevent pathological behaviour at near-OOM," I've definitely found this to be the case. My desktop Linux machine has no swap, and when I've run into memory pressure the OOM killer just swiftly terminated the program that was using all my RAM (I later bought more). When I've used machines with swap, my experience has been the OOM killer was much less aggressive. As a result you're left with an unresponsive system blocked on disk I/O.
- webstrand 5y agoThat's interesting, I've have just the opposite experience. I used to run with no swap; when compiling with many parallel processes, my system would sometimes live-lock. The OOM killer never triggered, and as far as I could tell, the issue actually was I/O saturation caused by thrashing on the few pages that the kernel _could_ evict. After enabling swap, I've never encountered this issue again. The machine remains interactive, if slow, under high load. Anecdotally, SIGSTOP-ing some processes when memory pressure is high, even without swap, seems to have some benefit. I'd resume those batch processes later.
- jcelerier 5y agoAnd I had the exact opposite experience as you just last week on multiple machines with swap enabled: compiling and going over the available memory made them entirely inoperable and required hard resets. Had to use another computer to be able to browse internet at the same time, until earlyoom can be installed there.
- mypalmike 5y ago"Under no/low memory contention... With swap: We can choose to swap out rarely-used anonymous memory that may only be used during a small part of the process lifecycle, allowing us to use this memory to improve cache hit rate, or do other optimisations." How do you improve cache hit rate when there's no memory contention? And what other optimizations? "Without swap: We cannot swap out rarely-used anonymous memory, as it's locked in memory. While this may not immediately present as a problem, on some workloads this may represent a non-trivial drop in performance due to stale, anonymous pages taking space away from more important use." When there's no memory contention, what's the "more important use"? These are honest questions - I'm not an expert on the topic.
- sgtnoodle 5y agoI think the point is that, the more free RAM you have, the more data the OS can speculatively read from disk when there's otherwise unutilized bandwidth. If a program happens to need the data, then it's already in RAM and the access latency is almost zero, vs. the non-zero latency of waiting for a hard disk platter to spin around. The claim being made is that there are certain situations where processes allocate a lot of physical memory but then very rarely accesses it. If it gets paged out to swap, then there's a latency hit on the next access. If the OS can avoid more latency hits total using the same physical RAM for caching, then the computer could do more overall calculations per second.
- Sirened 5y agoYep! The idea of "free memory" largely doesn't exist in modern operating systems. We haven't gotten to the point of power gating DRAMs (have we?), so we're always paying the cost of the DRAM all the time. As such, not using it means you're just throwing energy away. Keeping lots of purgeable data in these unallocated space like (as you said) disk cache not only speeds up access time but also saves a ton of power since a hit to DRAM is leagues lower than having to access external storage. While this is a non-trivial engineering problem, it's almost always a good trade off since it boost performance and saves power without any extra cost other than the effort of speculative loads (which, when linear, is much less expensive than a random one off load!) and managing it
- ornxka 5y agoSwap has always been so slow for me that I just disable it on all of my machines. I would rather the OOM reaper just SIGKILL whatever is using all my RAM than deal with slowness (which often persists after the OOM situation is gone).
- linsomniac 5y agoYes, but that's not quite how it works. As mentioned in this article, without swap, the system can live-lock before the OOM killer can take care of things. This has been my experience as well. I had hoped that getting rid of swap would prevent thrashing, but instead the system would live-lock.
- Sirened 5y agoI feel like the kernel live locking in low memory environments should be treated as a bug and not something we try to solve using swap. Like...if the OOM killer can't free memory under low memory constraints, something has gone seriously wrong
- nyanpasu64 5y agoI've tried twice to update Lakka (a custom Linux image that runs RetroArch and emulators) on a RPi 3B with 1 gigabyte of memory (and I think no swap). Updating Lakka from the Pi itself invokes a script which downloads a file from the network to SD card storage. This should not take up unbounded amounts of memory, but seems to trigger OOM livelock or something anyway (I can't tell what it is, since the GUI and SSH session hang, and Lakka doesn't enable Alt+SysRq or TTYs for some godforsaken reason). Enabling SSH and running `watch -n1 free -h` shows a concerningly low free memory amount, and IIRC the available memory number crept downwards before the GUI and SSH session both hung simultaneously. Terminating the GUI and performing an offline update from one SSH session while running `watch -n1 sync` avoids the hang. I haven't tried sync while leaving the GUI running. Will Linux OOM handling ever be fixed?
- tsimionescu 5y agoThe problem is the OOM killer only runs if no pages can be freed. But, on any system, there is some amount of memory that can be swapped out: code pages can be swapped out to their executable/library files on disk. On a system only running a few processes, this is unlikely to matter. But if you have a lot of processes, you may end up in a situation where you can free enough pages by swapping them all out to disk. Now you have no OOMKill, but the next time a process gets to run, you will stall until it is swapped in from disk. Then, the next process will be scheduled, and it too will need to be swapped in, causing another stall, and so on. The machine will probably end up executing, at best, a few instructions per millisecond for hours...
- throw0101a 5y agoSee also "Do we really need swap on modern systems?" (2017): * https://news.ycombinator.com/item?id=13715249 https://news.ycombinator.com/item?id=13715249
- deleted 5y ago[deleted]
- kijin 5y agoThis is an area where everyone's anecdotal evidence is different, because people are running all sorts of different workloads on all sorts of different RAM/swap/disk configurations. Naturally, some people will have had consistently positive experiences with swap, while others will have come out with consistently negative impressions. 32GB RAM + 512MB swap backed by a fast SSD is very different from 2GB RAM + 512MB swap backed by spinning rust. JVM is different from MySQL, which again uses memory differently than PostgreSQL. And yet we are so eager to hurl sweeping generalizations at each other, holding religiously onto our own subjective cargo cults. :(
- mhitza 5y agoFor Linux on desktop, my experience with swap used to be extremely bad before systemd-oomd. I got those system lockups easily in various workloads that for the longest time I went without swap, and a crashing program was a better experience. On the server, sprinkling in a bit of swap was standard practice in some orgs. As under load I could still log into a slow server and do something about it. My current problem with swap in Linux is that they make bad default choices. While a swapfile isn't optimal, allocating a slice of the disk for swap complicates my life when I do ram upgrades. Hibernate being tied to configured swap on a slice of disk (ugh), I just didn't even bother with it since upgrading ram.
- pmontra 5y agoYou are right. My own anecdotal experience: Rails, Django, Elixir/Phoenix web development, sometimes with docker. No swap on my 32 GB laptop both when I used it with a 750 GB HDD in its early years and with two 1 TB SSDs now. I'm usually using about 20 GB RAM because I'm leaving every single project open in its own virtual desktop, to get there quickly if a customer needs something done. No problem whatsoever that I remember. I'm never close to filling up RAM, that could be the reason. I'd buy extra RAM instead of enabling swap. The price would be extra battery depletion during the night.
- ggm 5y agoI never liked the OOM reaper. What it didn't do, was ask me what I wanted to kill. What it decided to kill, was usually something I didn't want it to kill, like X
- throwawayboise 5y agoYep. sshd was another favorite target. I'd rather it just crashed the system and rebooted than doing what it does. Ideally I'd want to tell it: first, kill java. Next, python. I think this is possible but I never was able to understand how to do it.
- ggm 5y agokill java, and keep on killing java with fire. Then move on to the printer daemon, the 200 vtys you forked on console I never use, the mouse daemon, and maybe in the end, sendmail.
- cwalv 5y agoThere's a more configurable userspace alternative: https://github.com/facebookincubator/oomd https://github.com/facebookincubator/oomd
- bmn__ 5y agoUse `earlyoom`. It comes with a sane whitelist and blacklist. https://news.ycombinator.com/item?id=27218036 https://news.ycombinator.com/item?id=27218036
- phendrenad2 5y agoSounds like both using swap and not using swap are common workarounds for some problem the kernel couldn't handle properly.
- j3th9n 5y ago4GB swap on a 4GB RAM VPS allows me to do an occasional yarn build, which would otherwise crash the server and who doesn’t need 8GB RAM the rest of the time.
- jbritton 5y agoI find swap and memory usage in general confusing. On my Windows machine, Chrome is listed as using about 1GB of RAM and I have maybe 50 tabs open. Seems like a lot. I think it’s possible that Chrome creates 50 processes and that the memory of DLLs loaded get over counted 50x. So 20MB of actual RAM DLL usage could be listed as 1GB. However maybe the DLLs are say 10MB and then Chrome would be using 500MB of data of 500MB of code. This might help with switching tabs and instantly seeing a page. For me though none of the tabs data need caching. I would prefer they just reload when needed. So I have this confusion. How much RAM is Chrome using? How much is truly needed vs hopefully fortuitous caching. Can some data be marked more important or is it all just LRU? Should Chrome free all memory on non-visible tabs? How do I know how much RAM to buy when I don’t know what anything really needs. Process Manager shows 8GB used of 16GB, but I currently have very little running. Chrome, Outlook, Ring Central. How would one know if this could run with 4GB or 8GB? Do you just have to try it and see if it seems sluggish. How does one distinguish memory used by code vs data?
- hinoki 5y agoTo answer your Chrome question, it’s open source and the tab discarding code is pretty straightforward. GetSortedLifecycleUnits sorts using GetSortKey, which for tabs is their last used time. So it’s just LRU. But some comments hint that maybe something smarter will happen eventually. As for your overall question, there are no easy answers. Deciding if memory is being used well, or could be repurposed to do something more useful is a difficult question to answer. https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/resource_coordinator/tab_manager.cc;l=393?q=DiscardTabImpl&ss=chromium https://source.chromium.org/chromium/chromium/src/+/main:chr... Edit: also, it’s really hard to reason about how much memory you need unless it’s very well defined (you know the size of data, exactly what processes to run), once you get to desktop operating systems, it’s more run and find out than reasoning from first principals. Disclosure: I work on ChromeOS.
- 0xdeadb00f 5y ago1GB for 50 tabs seems reasonable to me considering for me it sucks around the same amount of memory for like 15-20 tabs.
- deleted 5y ago[deleted]
- Animats 5y agoPart of the problem, and perhaps the main justification for swapping today, are all the programs which run infrequently but don't use the OS features to get themselves started on demand or periodically. On Linux, do "ps axl"; on Windows, use Task Manager. Look at all that stuff. There has to be some way to evict those memory hogs.
- fswd 5y agoputting swap on a ramdisk is faster than without swap and with swap. yes, it seems weird, yes it seems broken.
- scottlamb 5y agoArticles like this further cement my belief that Linux's low memory handling is deeply flawed. (macOS's too, btw.) My experience with Linux machines that have HDDs and swap is that once they start swapping, they slow down not only until memory is freed (ctrl-C gets through, OOM killer runs, whatever) but actually until reboot. My theory is that processes' VM space is now a minefield. Both anonymous memory and file-backed mmaps now have many gaps which get paged in on demand with inadequate readahead. With each seek taking ~10 ms, it doesn't take too much of that before you should give up and just reboot. Once memory is available, I'd like the system to proactively page back in both swap and file-backed pages to prevent this. If it does need to happen reactively, it should be in big blocks (each 10 ms of seeking should be follow by at least 10 ms of reading => ~1 MiB, not ~4 KiB or whatever it's doing now). By default. I'm not convinced the decision to use swap is ever a good one. E.g., from the article: > With swap: We can choose to swap out rarely-used anonymous memory that may only be used during a small part of the process lifecycle, allowing us to use this memory to improve cache hit rate, or do other optimisations. This is short-sighted. Programs can be written to tolerate their filesystem I/O being slow. They often take care to do I/O on a particular thread which is not handling network or GUI operations. They can't realistically be written to tolerate arbitrary memory accesses (including program text, stack, and heap) being that slow. It's a stupid idea to potentially slow down the latter to (try to) speed up the former. And programs shouldn't all have to mlockall, which iirc has other bad consequences. (maybe it allocates backing pages for all mappings immediately? even stuff like stack guard pages that's pure waste?) I have written programs to mlock (just) their program text; I'd prefer that weren't necessary either. I prefer that under memory pressure, a task dies promptly. Ideally the "right" task, but really it just has to be better than making the system unusable for a long time. Even if it kills the display server or pid 1 inappropriately, I was probably going to have to reboot anyway, so...
- xxs 5y agoSwap is exceptionally evil with any managed language (e.g. Java/C#) - running the GC over swap is effectively indistinguishable from a hard crash... except it takes forever. I have not used swap for over 20y, and never "missed" it.
- zozbot234 5y agoTo be fair, concurrent GC running in a separate thread should be enough to address this issue.
- ogogmad 5y agoYou do need swap if you're running Linux on a laptop! If you want Hibernate to work on your Linux laptop, you'll need to have a swap partition with space equal to (or bigger than) your RAM. Apparently, while there is a way to avoid setting aside a partition by using a swap file instead, it doesn't seem to work as well as using a partition. Hibernate seems to work OK on my current laptop running Linux * and using a swap partition. I've had poorer experiences with previous laptops though. Now that I'm sure it's possible, having functioning "Hibernate" is a requirement for me when buying Linux laptops. * - A LaptopWithLinux laptop running up-to-date Manjaro.
- chrismorgan 5y agoHibernate is not a requirement for everyone. First time I set up Arch Linux, I just never got round to setting up swap, and never felt any need for it either. Over the course of three years, I ran into OOM trouble four times (8GB of RAM was a lot in those days…). On my current laptop, I have a swap file and hibernation works fine (because this time I wanted to be able to switch over to Windows occasionally briefly without losing my session). It’s definitely more fiddly to set up in the Arch DIY style, but it’s still just following a set of instructions, so a distro that didn’t have the DIY philosophy could set it up easily enough.
- chickenimprint 5y agoDo you use multiple drives on your laptop? Because a single EFI system partition might become corrupted if you boot a system while the other is hibernated. Please refer to https://wiki.archlinux.org/title/Dual_boot_with_Windows#Fast_Startup_and_hibernation https://wiki.archlinux.org/title/Dual_boot_with_Windows#Fast...
- Macha 5y agoI think this is a little overblown. The old kernel ntfs driver which nobody uses and which is read only anyway ignores the fact that a drive is mounted by windows, so you could read corrupted data from the ntfs drive mounted by windows if windows hasn't fully completed writes that are hibernated, but you can't fuck up the partition because Linux won't write anything. The user space ntfs-3g, which is the one everyone actually uses, will refuse to mount a hibernated windows drive. The same appears to be true for the new kernel ntfs3 driver, since it's mount options include an option to force mount a dirty drive. If you use the options to force mount a dirty drive read/write, then yes, all bets are off, but you have to have dug pretty deep into your config to find out how to do that and at that point you have to know what you're doing.
- albertzeyer 5y ago> Swap is a storage area for these seemingly "unreclaimable" [anonymous] pages that allows us to page them out to a storage device on demand. This means that they can now be considered as equally eligible for reclaim as their more trivially reclaimable friends, like clean file pages, allowing more efficient use of available physical memory. I don't see that argument. Why should anonymous pages be treated equally? And executable pages / program text, maybe those also never should be swapped out? Is this configurable? This is usually more the behavior I would want on my system. I want that the OOM killer just kills sth directly. I remember that Google also has some custom handling for OOM on Linux. Although I don't find much information now on it, except: https://lwn.net/Articles/432223/ https://lwn.net/Articles/432223/
- roelschroeven 5y ago> And executable pages / program text, maybe those also never should be swapped out? Is this configurable? This is usually more the behavior I would want on my system. Aren't they? It's always been my believe that when such pages are reclaimed, they are not swapped out because they can be read back from disk anyway. Don't these pages fall under what the author means by "clean file pages"?
- jillesvangurp 5y agoSwap implementations have gotten a lot more sophisticated and complicated over time. But fundamentally it's an undesirable thing to be dependent on it as it is just a lot slower than normal memory. Part of the problem is that there is a lot of magical thinking rooted in decades of half truths about swapping on various operating systems with completely different implementations. Most of those per-conceptions if they ever were true at all, are now obsolete. For example, Windows XP used to have swapping. It commonly sucked the life out of any PC it was on. For no good reason whatsoever. Symptoms: the system performance would degrade over time and people would routinely reboot to fix that. Even with plenty of memory on a system, it would spend lots of time swapping things in and out of memory. Very infuriating if you just spent a lot of money on RAM. It was optimized for people with cheap hardware. Computers with enough memory were an afterthought. So it pretended you were running out of memory even when you weren't. Easy solution: disable swap completely; especially on machines with more memory than the total addressable memory per process on 32 bit windows XP (2 GB). I remember having a particularly slow windows laptop with 4GB of RAM. My life improved massively after I disabled swap on that thing. Alt tab was instantaneous; always. I could get individual processes to crash with an out of memory error but only if I deliberately opened way more processes than fit in memory (so, don't do that). Exactly the kind of thing that would make the laptop completely unusable with swap on in any case. I actually preferred the quick crash. Trying to close a non responsive application to free up some memory while the system is 100% engaged with grinding your disk to fine dust is not fun. Linux is way more complicated and it depends on the use case. A swapping server is a dead server. So, you should be conservative with any kind of swap there. Basically, if you have enough memory and no memory leaks there should be no need for it to degrade response times (aka swap), ever. If you do have memory leaks, swapping is actively harmful because it degrades response times. Better reboot the server. Or rely on the docker oom killer. Or assign more memory. Spin up more servers. Etc. And of course you monitor memory usage on servers to preempt any kind of issues. Swapping is the punishment that happens on a poorly, configured un-monitored server. You should have no need for it. For a laptop with not enough memory because the buyer tried to save some cash and bought something with a pitiful amount of memory, definitely have swap space available. Performance will suck but at least it will be able to run some software. And Linux isn't as bad at this as Windows XP used to be. People actually care. Otherwise, you probably want it to be able to hibernate and you need swap space for that. But perhaps with a very low swappiness value so that it won't actually swap. Or in case you don't need hibernate, turn it off completely and save some disk space.
- crazypython 5y agoI agree with this post, though there's another factor to consider: SSD endurance. A system that swaps a lot will write more, decreasing endurance. You might need to buy a new SSD every 5 years.
- rwmj 5y agoI'd like to know how swap-on-zram changes things. Fedora has started to enable zram on new installations (https://fedoraproject.org/wiki/Changes/SwapOnZRAM https://fedoraproject.org/wiki/Changes/SwapOnZRAM) and disabled swap partitions.
- nickcw 5y agoIn my experience swap to zram works really well. I have 16GB of RAM in my linux laptop running Ubuntu and it isn't quite enough: Chrome, Virtual box, gopls and thunderbird regularly push it over the edge into swap death needing a reset to recover. My first install was the earloom killer a user space oom killer. This has a nice desktop notification and when things got tight I'd get notifications about it killing chrome processes. That made things liveable with but not perfect. I couldn't run Zoom without earlyoom killing it. I finally added swap to zram and that has helped enormously meaning I have enough ram to run everything I need without the earlyoom kicking in regularly. Anyway I'm just about to upgrade to a new laptop I can put more than 16GB of ram in, but I think I'll leave both the earlyoom and the compressed swap in place.
- grishka 5y agoThe one thing that used to drive me crazy, especially about Windows, was "why the hell are you using swap and why are you freezing to do stuff with the hard drive on each click when you have a third of the physical RAM free right here according to your own task manager". I still don't understand that. I still feel like modern desktop OSes are terrible with memory management when you run out of physical memory (which I did way too often on all my previous machines).
- VMtest 5y ago1. https://news.ycombinator.com/item?id=26244093 https://news.ycombinator.com/item?id=26244093 | M1 Mac high SSD writes Not sure if the above case has been solved in M1 Mac On x86 Mac, if the programs exceed the memory available, it would start swapping to disk too, though that is not as how Windows do swapping. On Windows, if I don't enable pagefile swap, some programs will crash even if there are so much memory available Anyway, I do monitoring on memory usage on any OS that I use. Out of memory situation by people mostly is due to negligence, it's what it is. If people are unhappy because doing memory monitoring would waste their focus energy, just buy more computers
- accelbred 5y agoWith the concerns stated in the article, I can't see any benefits to adding swap vs adding an equivalent amount of ram. The author mentions that not needing swap due to having more than enough ram is a common misconception, but I cannot see how that can be. For any given workload, you could replace the useful amount of swap for an equivalent amount of additional ram. Sure some pages cannot be reclaimed, but the amount of anonymous pages that can be reclaimed is limited by swap size. In an alternate system with more ram instead, the pages that would be in swap instead are locked in memory, but this does not give more useful memory. More ram would be more flexible as it allows more cache than the swap scenario when you dont have swap full of pages. Thus we can conclude that there will always some amount of ram that is sufficient, and therefore if your system has enough memory that you dont face memory contention, you don't need swap. If theres a better explanation for why swap is useful, I'd like to hear it, but the article is unconvincing.
- adrian_b 5y agoI also completely agree with everything you said. I have stopped using swap on all my servers, desktops and laptops about 20 years ago. I have never encountered any situation when swap could have been useful, but I have always installed generous quantities of RAM on all computers. As long as the quantity of used memory does not exceed the physical memory, swapping pages cannot improve the performance in any way. The only exception, which is mentioned in the parent article, is when some very seldom used memory pages would be swapped and the freed memory would be used as a disk cache, hopefully increasing the performance for a I/O bound application. Whether this strategy of hoping that a larger disk cache, filling all the available physical memory, can improve the performance more than what can be lost if the prediction that the swapped pages will not be needed soon is wrong, is extremely application dependent. This hypothesis, that a significant performance improvement can be enabled by a slightly larger disk cache, might happen to be true in certain cases on computers which still use slow HDDs. Except for backup/archiving, where the disk cache matters very little, I have not used HDDs for more than a decade. On computers with SSDs, the chances to find an application where a small increase in disk cache size can cause a noticeable performance increase, larger that what is lost by swapping the seldom used memory pages, are negligible, in my opinion.
- 5y ago
- thxg 5y ago(The article is from 2018.) I think the article is strongly from the point of view of what to do "in production". If you have a bunch of servers with known specialized workloads, I can believe that enabling swap is good for efficiency. You can run more tasks and get closer to the memory limit while remaining safe. Coming from a Facebook employee, this makes sense. However, for individual development machines, the pragmatic situation is completely different. The truth is that in practice running out of memory stems from two types of human errors: 1. You wrote some code that leaks lots of memory quickly. 2. You used Google Chrome. In either situation, still in 2022, Linux reacts in the following way: A. With swap, your system becomes completely unresponsive for longer than you have patience for, so you power-cycle it. B. Without swap, the culprit is immediately identified and killed. Your system is perfectly usable, aside from a killed process. Note that depending on your distro, the good case B. may have become a bit harder to achieve recently: systemd-oomd now tries to eagerly detect OOM situations before the kernel, and when it does, it kills the offending process' whole cgroup! If you have gnome or kde, you're good, but otherwise, this may terminate your session. This can be fixed with "systemctl mask systemd-oomd.service"
- iforgotpassword 5y agoFor me it works well with swap. I'm playing an indie game that suffers from a slight memory leak. After 4 or 5 hours the oom killer comes along and puts an end to it. Everything else gets pushed to swap before the game is killed. No unresponsiveness.
- simiones 5y agoI think case B is not so cut-and-dry. If the code is leaking slowly enough, you will encounter case C, where you don't have swap and the kernel starts swapping out your code pages, and then your computer slows to entirely completely unusable levels for far longer than in case A. Especially if you swap out to a relatively low latency disk (SSD), swap is much better than risking state C.
- deleted 5y ago[deleted]
- 5y ago
- vbezhenar 5y agoI consider swap very necessary unless Linux is used like platform for something like Kubernetes and then you need to obey its rules. I'm adding enough swap for all servers I manage and so far they rock stable. Good thing is that even if there's enough memory, some unused memory with time will migrate to swap and that'd free some RAM for disk buffers or consumption spikes. This behaviour seems somewhat broken on some modern distributions, so I prefer old distributions, I guess some defaults were changed which lead to less stable and performant systems in my observations.
- marginalia_nu 5y agoMy practical experience is that Linux absolutely needs swap to work well. It doesn't need a lot, but some. I had a ton of OOMs even though the memory wasn't "full" on the machine running my search engine. The machine had like dozens of gigabytes in "buff/cache" that could be reclaimed, but for some reason it failed and let loose the OOMKiller instead.. I added 512 Mb of swap and it just went away overnight. I've since increased the allocated memory by a lot more than 512 Mb.
- Oxodao 5y agoWhile I would not remove the swap on a production machine just to be safe, I've been running my home computers for ~5 years exclusively on Linux without swap. I've never encountered any of those issues for some reasons
- marginalia_nu 5y agoI usually don't have swap on my workstations either, which is why I initially thought I could get away with it on my server. Probably different usage patterns. I routinely memory map something like half a terabyte of data on this machine. Might also be due to huge pages, that feature seems a bit janky to be honest.
- deleted 5y ago[deleted]
- eitland 5y agoOpinions differs widely here. My observations: - I can get away with about half the memory on Linux vs Windows (significant contribution comes from not having to run VMs for docker and WSL, but also it seems applications and services use less memory on Linux.) - I always used to use swap. These days it seems defaults in distro setups pushes one away from swap. - For some reason I feel it worked better before (always w/swap). - These days I feel I see some problems either I use swap or not.
- kkfx 5y agoTwo sidenotes: - actual desktops, and some entry-level server are designed with much more CPUs that their usage need, and chronically too tittle ram. I'm curios if that's a classic "my CPU is big" vs and old reminiscence of the past were sw was far less resource hungry and CPU very slow... - storage is "cheap enough" to "wast" some in exchange of "buffer for peak loads", however until GNU/Linux do something less ugly for the OOM killer there might be still issues...
- gradschool 5y agoArticles like this make me feel really out of touch. Honest questions: has it become standard practice to write applications that don't handle heap overflow at all because it's meant to be handled at the kernel level by specialists such as the author? As an alternative to using an OOM killer, why doesn't the kernel stop granting memory when it runs out and let the application that requested it sink or swim?
- dundarious 5y agoOther OSes such as Windows do the latter. Linux "overcommit" is sometimes problematic, but has been the lay of the Linux land for quite some time. I don't love it.
- ypsu 5y ago> Disabling swap does not prevent disk I/O from becoming a problem under memory contention, it simply shifts the disk I/O thrashing from anonymous pages to file pages. This is my pet peeve when using linux as desktop. Does anyone know if this can be disabled entirely? Something like mlockall(MCL_ONFAULT) but for all the processes. I wish there was a knob for this (e.g. vm.swappiness=-1) but I haven't found one. :( On my desktop the most common problem I have is that I have a runaway binary or a runaway chrome tab. 90% I simply want that killed so swap is not being helpful. Unfortunately it takes ages for the OOM killer to activate. I have indicators for how much free memory I have left, and I can manage accordingly. I'd prefer a snappier desktop than pretending I have infinite memory. If I feel I have too little memory, I can just buy more, but that still doesn't protect against linux locking up if you accidentally use up everything. Here's a simple repro: // gcc -std=c99 -Wall -Wextra -Werror -g -o eatmem eatmem.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char** argv) { int limit = 123456789; if (argc >= 2) limit = atoi(argv[1]); setbuf(stdout, NULL); for (int i = 1; i <= limit; i++) { memset(malloc(1 << 20), 1, 1 << 20); printf("\rAllocated %5d MiB.", i); } sleep(10000); return 0; } And here is how you use it: $ gcc -std=c99 -Wall -Wextra -Werror -g -o eatmem eatmem.c $ ./eatmem Allocated 31118 MiB.Killed $ ./eatmem 31110 Allocated 31110 MiB. Without args it keeps allocating RAM until the kernel kills it. Basically it tells you how much free ram you have. The second time you ask it to allocate a little less to create a near-oom condition on your machine. This pretty much locks up my machine (e.g. the mouse pointer doesn't budge) and I have to invoke the kernel OOM killer to recover (Alt+SysRQ+f). I don't use swap. If I could somehow disable the file paging then I could avoid this problem. (I really don't care how "efficient" file paging is, I want a snappy system instead!)
- zozbot234 5y ago> This is my pet peeve when using linux as desktop. Does anyone know if this can be disabled entirely? Something like mlockall(MCL_ONFAULT) but for all the processes. I wish there was a knob for this (e.g. vm.swappiness=-1) but I haven't found one. :( Looks like the supported way of doing this is to use the vmtouch(8) utility to ensure that the files (or file pages) you care about are resident in RAM. There's also a fadvise utility, packaged within fcoretools, that seems to be similar.