by seppel · 2y ago · 0 comments
And I have time to think about better problems than "You can achieve better swap behaviour under memory pressure and prevent thrashing by utilising memory.low and friends in cgroup v2."
The core problem with swap is that nowadays you need gigabytes of swap to actually make a difference (when you have 32GB of RAM, 1GB of swap is 1/32 of a difference). And you certainly don't want to system to swap in and out gigabytes of memory.
Swap is less critical now, and given that k8s has some pathological dislike of it (again I suspect based on vibes) I can see why people dont have it.
However when you are using close to 70% of your total ram, swap improves performance significantly. Also where I work, it also stop the OOM from killing my repo VFS layer when I'm compiling something.
> The core problem with swap is that nowadays you need gigabytes of swap to actually make a difference
It was always the case. but then a TB of disk isn't that expensive anymore.
Yes, but the author is most likely optimizing for a metric that I don't care so much in my per personal computer while dismissing metrics that I care about.
> It was always the case. but then a TB of disk isn't that expensive anymore.
But swapping out GB of RAM is expensive in terms of latency.
But it has to be GB to actually make a difference. If your swap is not full of GB of data, then obviously you don't need it (or it just becomes a reserve when you are in an OOM situation - but this is where you don't want to be with swap avaible either)
I have stopped using swap on any computers, laptops, desktops or servers, more than 2 decades ago.
At that time, removing the swap was definitely a great improvement (with the condition of having installed enough real memory in the computers; but even with insufficient memory it was much better to have a few processes killed instead of having an unusable computer that had to be forcefully rebooted, losing everything that had not been already saved).
Perhaps the handling of swap has been improved meanwhile and in certain unusual niche environments having swap might provide some benefits.
Nevertheless, I doubt very much that this is true. I have never seen any such case where swap could have been useful.
When there is enough memory, there should be no writes to the swap, because such writes can only diminish the performance, and in an unpredictable way, because they can trigger the SSD garbage collector. If there are no writes to the swap, there is no reason for it to exist. If there isn't enough memory, the cheapest solution is to buy more memory, instead of trying to find a software workaround for that.
The memory pages that are not dirty can always be discarded by the operating system and read again later from the SSD if needed. No swap is needed for that. There exists no way in which swap can improve performance in comparison with having enough memory. Having to also discard dirty pages is just another way of saying that there isn't enough memory and you are willing to degrade the performance instead of paying for enough memory.
Swap memory is certainly never needed in personal computers, outside of temporary use in certain exceptional situations, when one would be willing to run very slowly a program that does not fit in the memory, due to lack of access to a computer with more memory.
Swap could be useful only when configured in a great number of servers that run a well characterized workload, where one could make a trade-off between application performance and total memory cost.
In a server environment you don't have the luxury of running your 4 GB needing app on a 128 GB machine - that means you have overprovissioned and are paying a stupid tax.
A modern server aims to run as close as possible to 100% CPU and RAM usage.
In those cases you need swap to kick out rarely used memory (initialization, ...)
Why though?
Swapping 32GB of memory in and out of an NVMe SSD is much faster than swapping 1GB out of a spinning disk so I really don't get that argument.
This is true, but it still takes time. What kind of workload do you have where you swap out 32GB of RAM? If you are in this situation you almost certainly need to buy these additional 32GB of RAM.
As an example of such workload: a server, one of the many, that runs financial calculations from many web users. It's fine for it to become somewhat slower: we'll notice the performance degradation in the metrics and stop accepting new connections for a while. It is not fine for it to just die with OOM ― the users automatically will reconnect to other servers, leading to a sort of a thundering herd scenario.
Then you obviously have a workload where having swap make sense (and you could even add swap on the fly in such a case). But that is not typical at all.
If you have enough money to provision NVMe and still swap multiple gigabytes, just get a 16GB RAM stick for $40.
Laptops come with soldered RAM now where upgrades are not possible. And most people use laptops nowadays instead of desktops.
Buying another laptop to double your ram just because you hit the swap once in a blue moon is kinda wasteful.
Why should I pay $40 when I have way more than 16GB of free SSD storage on my computer already?!
> it is still more than an order of magnitude slower than RAM, and is harmful for the disk cells.
I mean, if that's not an issue for you, be my guest. But that's not the global optimum.
I treat my 1TB SSD like a loaner and after doing the math, based on my write patterns in the last 2 years, the SSD should wear out in about ..checks notes.. 12 years. I'm a lot more likely to replace it 2-4 years though to upgrade to a bigger and faster model anyway.
Plus, all storage dies eventually, that's why backups are important. You paid for it, you might as well make the most use of it while it lasts instead of trying to "hypermile it" since you won't be leaving it as inheritance to your grandchildren.
I think swap is useful as a temporary memory overflow area, but that's it. Actually "running relying on swap" is harmful
Items 1 and 2 on the list sound like fiction. Item 4 sounds plausible (same for the Windows 9X series - sigh - where cache and swap would allegedly fight each other)