5 ms·
The swap/memory situation in linux has surprised me quite a bit coming from Windows. Windows remains mostly fully responsive even when memory is being pushed t
by Numerlor 7mo ago
The swap/memory situation in linux has surprised me quite a bit coming from Windows.
Windows remains mostly fully responsive even when memory is being pushed to the limits and swapping gigabytes per second, while on linux when I ran a stress test that ate all the memory I had trouble even terminating the script
- jauntywundrkind 7mo agoLike Linux / open source often, it depends on what you do with it! The kernel is very slow to kill stuff. Very very very very slow. It will try and try and try to prevent having to kill anything. It will be absolutely certain it can reclaim nothing more, and it will be at an absolute crawl trying to make every little kilobyte it can free, swapping like mad to try options to free stuff. But there are a number of daemons you can use if you want to be more proactive! Systemd now has systemd-oomd. It's pretty good! There's others, with other strategies for what to kill first, based on other indicators! The flexibility is a feature, not a bug. What distro are you on? I'm kind of surprised it didn't ship with something on?
- 01HNNWZ0MV43FF 7mo agoI've had that same experience. On new systems I install earlyoom. I'd rather have one app die than the whole system. You'd think after 30 years of GUIs and multi-tasking, we'd have this figured out, but then again we don't even have a good GUI framework.
- LtWorf 7mo agoI used to use it but it's too aggressive. It kills stuff too quickly.
- dlcarrier 7mo agoThere's two things that cause this. First, Windows has a variable swap file size, whereas Linux has a fixed size, so Windows can just fill up your drive, instead of running out of swap space. Second, the default behavior for the out-of-memory killer in Linux isn't very aggressive, with the default behavior being to over-commit memory instead of killing processes. As far as I know, Linux still doesn't support a variable-sized swap file, but it is possible to change how aggressively it over-commits memory or kills processes to free memory. As to why there differences are there, they're more historical than technical. My best guess is that Windows figured it out sooner, because it has always existed in an environment where multiple programs are memory hogs, whereas it wasn't common in Linux until the proliferation of web-based everything requiring hundreds of megabytes to gigabytes of memory for each process running in a Chrome tab or Electron instance, even if it's something as simple as a news article or chat client. Check out this series of blog posts. for more information on Linux memory management: https://dev.to/fritshooglandyugabyte/series/16577 https://dev.to/fritshooglandyugabyte/series/16577
- LargoLasskhyfv 7mo ago> As far as I know, Linux still doesn't support a variable-sized swap file... You can add (and remove) additional swapfiles during runtime, or rather on demand. I'm just unaware of any mechanism doing that automagically, though. Could probably done in eBPF and some shell scripts, I guess?
- dsr_ 7mo agoswapspace (https://github.com/Tookmund/Swapspace https://github.com/Tookmund/Swapspace) does this. Available in Debian stable.
- LargoLasskhyfv 7mo agoWow. Since 20 years. And I'm rambling about eBPF...
- dlcarrier 7mo agoLinux's ePBF has its issues, too. I once was trying to set up a VPN that needed to adjust the TTL to keep its presence transparent, only to discover that I'd have to recompile the kernel to do so. How did packet filtering end up privileged, let alone running inside the kernel? I recently started using SSHFS, which I can run as an unprivileged user, and suspending with a drive mounted reliably crashes the entire system. Back on the topic of swap space, any user that's in a cgroup, which is rarely the case, can also crash the system by allocating a bunch of RAM. Linux is one of the most advanced operating systems in existence, with new capabilities regularly being added in, but it feels like it's skipped over several basics.
- Joker_vD 7mo agoWindows "figured it out sooner" because it never really had to seriously deal with overcommitting memory: there is no fork(), so the memory usage figures of the processes are accurate. On Linux, however, the un-negotiable existence of fork() really leaves one with no truly good solution (and this has been debated for decades).
- rwmj 7mo agoThe annoying thing I've found with Linux under memory stress (and still haven't found a nice way to solve) is I want it to always always always kill firefox first. Instead it tends to either kill nothing (causing the system to hang) or kill some vital service.
- pmontra 7mo agoI'm not sure that I'd want the OS to kill my browser while I'm working within it. Of course the browser is the largest process in my system, so when I notice that memory is running low I restart it and I gain some 15 GB. Basically I am the memory manager of my system and I've been able to run my 32 GB Linux laptop with no swap since 2014. I read that a system with no swap is suboptimal but the only tradeoff I notice is that manual OOM vs less writes on my SSD. I'm happy with it.
- robinsonb5 7mo agoThere are two pillars to managing RAM with virtual memory: the obvious one is is writing one program's working set to disk, so that another program can use that memory. The other one - which isn't prevented by disabling swap - is flushing parts of a program which were loaded from disk, and reloading them from disk when next needed. That second pillar is actually worse for interactivity than swapping the working set, which is why disabling swap entirely isn't considered optimal. By far the best approach is just to have an absurd amount of RAM - which of course is a much less accessible option now than it was a year ago.
- jauntywundrkind 7mo agoIf using systemd-oomd, you can launch Firefox into it's own cgroup / systemd.scope, that has memory pressure control settings set to not kill it. ManagedOOMPreference=avoid. https://www.freedesktop.org/software/systemd/man/latest/systemd.resource-control.html#Memory%20Pressure%20Control https://www.freedesktop.org/software/systemd/man/latest/syst... There's a variety of oom daemons. bustd is very lightweight & new. earlyoom has been around a long time, and has an --avoid flag. https://github.com/rfjakob/earlyoom?tab=readme-ov-file#preferred-processes https://github.com/rfjakob/earlyoom?tab=readme-ov-file#prefe... Your concerns are very addressable.
- Onavo 7mo ago> Windows remains mostly fully responsive even when memory is being pushed to the limits and swapping gigabytes per second In my experience this is only on later versions of the NT Kernel and only on NVME (mostly the latter I think).
- robinsonb5 7mo agoYeah I think SSD / NVME makes all the difference here - I certainly remember XP / Vista / Win 7 boxes that became unusable and more-or-less unrecoverable (just like Linux) once a swap storm starts.
- p_ing 7mo agoNT4 exhibits the same behavior under extreme load.
- nasretdinov 7mo agoIn Linux the default swap behaviour is to also swap out the memory mapped to the executable file, not just memory allocated by the process. This is a relatively sane approach on servers, but not so much on desktops. I believe both Windows and macOS don't swap out code pages, so the applications remain responsive, at the of (potentially) lower swap efficiency
- superjan 7mo agoDon’t know about Macs, but on Windows executable code is treated like a readonly memory mapped file that can be loaded and restored as the kernel sees fit. It could also be shared between processes, though that is not happening that much anymore due to ASLR.
- inkyoto 7mo ago> In Linux the default swap behaviour is to also swap out the memory mapped to the executable file, not just memory allocated by the process […] I believe both Windows and macOS don't swap out code pages, so the applications remain responsive, at the of (potentially) lower swap efficiency Linux does not page out code pages into the swap. You might be conflating page reclamation with swapping instead. In Linux, executable «.text» pages are mapped[0] as file-backed pages, not anonymous memory, so when the kernel needs to reclaim RAM it normally drops those pages and reloads them from the executable file on the next page fault once they are accessed again (i.e. on demand) rather than writing them to swap. In this particular regard, Linux is no different from any other modern UNIX[1] kernel (*BSD, Solaris, AIX and may others). [0] Via mmap(2) in argv[0], essentially. [1] Modern UNIX is mid-1990's and onwards.
- nasretdinov 7mo agoYes, you are correct, I wasn't precise enough. It doesn't make sense to swap the existing code pages, they are just unmapped. (And that's the reason why you get "text file busy" when doing scp over the file: since the OS relies on the fact that the .text pages can be safely unmapped it needs to guarantee that they stay read-only)
- man8alexd 7mo ago
- demosito666 7mo agoOh yeah. Bug 12309 was reported now what, 20 years ago? It’s fair to say that at this point arrival of GNU Mach will happen sooner than Linux will be able to properly work under memory pressure.
- TacticalCoder 7mo ago> Windows remains mostly fully responsive even when memory is being pushed to the limits and swapping gigabytes per second ... The problem problem though is all the times when Windows is totally unusable even though it's doing exactly jack shit. An example would be when it's doing all its pointless updates/upgrades. I don't know what people are smoking in this world when they somehow believe that Windows 11 is an acceptable user experience and a better OS than Linux. Somehow though it's Linux that's powering billions if not tens of billions of devices worldwide and only about 12% of all new devices sold worldwide are running that piece of turd that Windows is.