by utf_8x · 2y ago · 0 comments
Joking aside, is there an actual legitimate reason to do this on a workstation? I understand why you would want to disable swap on something like a kubernetes cluster node but in my head, heaving at-least zram enabled is a good thing on a workstation so you *don't* get OOM killed... I call on thee, Linux wizards of HN, to help me understand the reasoning behind this.
This was always in a setup where I'd have ample RAM for my everyday tasks, and was doing numerics. Running OOM would invariably mean two things:
1. I had a bug in my scripts, which typically meant I'd accidentally materialized a huge sparse matrix or some such, and thus
2. The system wouldn't go "just a little" OOM but rather consume memory an order of magnitude over the actual system's capacity. And it would not recover.
In that scenario, the system would typically start swath-thrashing so hard that I'd just cold reboot. An OOM daemon fixed that and let me iron out my bugs.
This means there's a lot of available RAM capacity, that there's a hefty read cache to avoid the SD card, that when there are disk writes on writable storage it can still read from it, and with the lack of clustering and the speed of decompression there's no swapping lag whenever a page needs to be swapped back. This swap early, swap often is the complete opposite of the OOM-prevention swapping you used to use on disks, which was slow and interrupted IO whereas LZ4 in RAM is fast and doesn't interrupt IO.
I have been using this setup since 2022 and have not had any issues but I don't compile anything on those setups, though I see no reason why it would not be safer than compiling without zram at all.
/etc/sysctl.d/99-240716-vm.conf
vm.dirty_background_ratio = 1
vm.dirty_ratio = 100
vm.page-cluster = 0
vm.swappiness = 200
vm.vfs_cache_pressure = 1
dirty_background_ratio = starts background writing when it's at least 1% of available mem; dirty_ratio = starts force writing when all avail (not total) ram is full; page-cluster = swap in only what's needed; swappiness = lower means swapping is expensive higher signals swapping is cheap; vfs_cache_pressure = lower keeps more dentries and inodes in memory. /etc/udev/rules.d/99-240716-ioschedulers.rules
ACTION=="add|change", KERNEL=="mmcblk[0-9]*|nvme[0-9]*|sd[a-z]*", ATTR{queue/scheduler}="none"
Removes schedulers from typical local writeable storage. /etc/systemd/zram-generator.conf
[zram0]
zram-size = ram
compression-algorithm = lz4
Might have to install systemd-zram-generator if it doesn't already exist.There used to be a time where swapping out meant moving cogs and wheels full of heavy rocks and RAM frequencies could be approximated by waving a stick until it made whistling noises. At that time suddenly dealing with memory swap made the system unusably unresponsive (I mean unusable, not just frustrating or irritating). Advice about disabling swap and zram came from that time for “resource constrained” systems. Unfortunately the meme will never die because the wikis and now regurgitated LLM drivel will just never let it go because nobody has gotten around to fixing it.
In my experience a machine is never going to recover when a workload pushes it into swapping because something has gone awry and that situation is not going to fix itself.
The most useful thing honestly has been a memory usage applet in the task bar. Memory leaks usually have a very clean and visible signature that provides a few seconds of warning to hit alt-tab-tab-ctrl-c.
After that improvement one can be swapping constantly and the machine is still responsive.
Linux memory management works well for servers where you can predict workloads, set resource limits, spec the right amount of memory, and, in most cases, don't care that much if an individual server crashes.
For workstations, it either kicks in too early (and kills your IDE to punish you for opening too many tabs in Chrome) or it doesn't kick in at all, even when the system has become entirely unresponsive and you have to either mash magic sysrq or reboot.
Interestingly that was my experience on steam deck with its default 1gb swap. But after enabling both zram and larger ordinary swap (now also default setting for upcoming release) it became much more stable and responsive.
Experience has shown me over and over that you just want to feel the limits of the machine hard and fast so you can change what you're asking of it rather than thinking that there is some perf issue or weird bug.
It's the idea that swap is somehow useful that's old. It's not, it never worked right for interactive systems. It's a mainframe thing that needs to die.
Lots of the stuff you're using is backed by disk anyway -- and will be removed from RAM when there's any memory pressure, whether or not you have any swap. If you've got swap then the system can put anonymous pages in it, otherwise it'll need to evict named files more frequently.
Unless you have enough RAM that you're literally never evicting anything from your page cache, in which case swap still doesn't hurt you.
I'll absolutely agree that swapping out part of the working set is unwanted, but most swapping is benign and genuinely helps performance by allowing the system to retain more useful data in RAM. You don't want to get into a state where you're paging code in and out of RAM because there's nowhere to put data that's not being used.
Other random semi-related thoughts:
- Rust having to define a new stdlib to be used in Linux kernel because of explicit allocation failure requirements. Why wasn't this possibility factored in from the beginning?
- Most software nowadays just abstracts memory costs, partly explaining why a word processor that used to work fine with 64mb of RAM now takes a gig to get anything done.
- Embedded development experience should be a requirement for any serious software engineer.
As the gas-powered engine people will say: "there's no replacement for displacement" (I wont push the analogy comparing zram to turbocharging but, you know, they both deal with "compression"...)
Like one thing I learned some time ago: swap-out in itself is not a bad thing. swap-out on it's own means the kernel is pushing memory pages it currently doesn't need to disk. It does this to prepare for a low-memory situation so if push comes to shove and it has to move pages to disk, some pages are already written to disk. And if the page is dirtied later on before needing to swap it back in, alright, we wasted some iops. Oh no. This occurs quite a bit for example for long-running processes with rarely used code paths, or with processes that do something once a day or so.
swap-in on the other hand is nasty for the latency of processes. Which, again, may or may not be something to care about. If a once-a-day monitoring script starts a few milliseconds slower because data has to be swapped in... so what?
It just becomes an issue if the system starts trashing and rapidly cycling pages in and out of swap. But in such a situation, the system would start randomly killing services without swap, which is also not entirely conductive to a properly working system. Especially because it'll start killing stuff using a lot of memory... which, on a server, tends to be the thing you want running.
Default configs of most distros are set up for server-style work, even on workstation distros. So they’ll have CPU and IO schedulers optimized for throughput instead of latency, meaning a laggy desktop under load. The whole virtual memory system still runs things like it is on spinning rust (multiple page files in cache, low swappiness, etc).
The only distro without this problem is Asahi. It’s bespoke for MacBooks, so it’s been optimized all the way down to the internal speakers(!).
LOL. A Ken Colivas problem, circa 2008, still there :-)))
I had a machine freeze this month because it was trying to zram swap, and have hit shades of the problem over the last few years on multiple machines running multiple distros. Sometimes running earlyoom helps, but at that point what's the point of swap? So no, this isn't out of date.
Today, don't buy a computer (regardless of size) with less than 32 GB of ram. Yes, this applies to fruity products as well. Part from making it a more enjoyable experience it will also extend the usable life of the computer immensely.
(The weird crap about apple computers not needing as much RAM comes from iOS vs. android and is for different reasons, does not apply to real computers)
And when your partner gets a new computer, for whatever reason, the old one can easily live on for many many years. But it's utility will be limited if it only has 8 GB of ram.
The product in the article is only 8 years old but already stretching its usefulness for no good reason.
With a 32GB memory, 256GB ssd-disk laptop, it would be really weird to set up 64GB of the disk for swap.
(also, a computer with 32 GB and 256 GB disk is a very weird combination not quite fitting a typical general purpose computer)
I am not sure why you would disable those in many scenarios.
htop froze, so i hit ctrl-c, but nothing happened. no mouse movement, no ssh'ing in, just totally hard-locked. i ended up having to physically powercycle the machine.
after that i turned off swap so that it killed the process rather than the machine (and remembered to pass -DLLVM_PARALLEL_LINK_JOBS=1)
Allocating 16/32/64/128GB of NVME storage to swap is mostly just a waste of disk space for me. When I had swap enabled, it was constantly showing 0 used. (Not "pretty much none", literally "0.0".)
Further, if I'm trying to use more than 64GB of RAM... I'm fine with things getting OOM killed. I don't know that I've ever had anything OOM-killed when something wasn't clearly misbehaving. (I count Chrome eating 50GB of RAM because I haven't closed any tabs all week as me clearly misbehaving for the purposes of this discussion.)
And as far as zram... I guess same sorta arguments. I'm not running out of RAM, so why use up CPU cycles (and presumably battery power)? why use up brain cycles setting that up?
Until I've maxed out my system's RAM, I'd rather just throw more RAM at it.
zram does basically nothing while your working set fits into memory, no performance penalty.
If your workloads involve using more RAM than you have you can... add more RAM, use swap/zram/etc, or just not do that thing.
Absolutely makes sense to me to throw some swap into the mix. I'd probably do the same if it were an infrequent use case (otherwise preferring to just add more RAM).
But also absolutely makes sense to me to not have any swap enabled on this machine right now.
My laptop has 64GB RAM and 1TB NVME, I run with swap off because I want all storage usable should ideally not be pressed for memory.
I also have memory and storage allocation in my task bar to easily monitor the situation.
On a machine with FOUR GIGABYTES OF RAM at that.