Comment by nvme0n1p1
3 days ago
Agree. I tried using a swapfile last year-ish, as the internet experts tell you to. I have 64GB of RAM. If I fresh boot and start a few apps I'll be using maybe 16GB RAM + 0GB swap. Then a few hours later I see I'm using 8GB of RAM + 8GB of swap... why? Why did Linux waste IO moving data to disk when there's no memory pressure?
That's why I don't use swap.
Generally the idea is that regions of memory that aren't being used are swapped out to disk, then other more useful things are brought into memory.
For example: you have a background program that you're not using and in the foreground you're crunching data from disk. In this scenario the most efficient use of your memory is to swap the background program out and pull the disk data into RAM.
It's a broken idea, throughput über alles isn't true of an interactive system, latency matters. There's a calculator app I use every now and then. Linux always thinks it's a good idea to swap it out since I'm not using it all the time, which means that every time I want to calculate something there's lag as it's swapped in again.
No sorry, its not a broken idea: throughput über alles is true of high-throughput-demanding systems, but latency matters is also true, for latency-demanding systems, and the two use cases represent - in an industrial sense - a very vast array of values. It's not really either/or - you are indeed identifying a knob that needs to be turned, in your particular use-case, on your particular system. There is no one-size fits all: it's a knob.
Another aspect I'd wonder about for relatively simple tasks is just freeing it from memory and being prepared to pay the cost of loading it again, it's probably already on disk. Latency matters, but it seems there will be limits especially considering the transfer speed and latency improvements we've seen to storage commonly installed in most systems over the past 15 years or so.
Then comes the challenge of deciding what processes/data to free or page, whether that's flagged by the system as it's running or at coding/compile time. And that seems similar to the issue of picking a swap strategy for a distro/OS, which will depend on the user's demands or system, setting a good default and exposing how to change the configuration to a better match if needed.
But why should it swap things out even when I'm not crunching data and there's no memory pressure? I'd rather everything stay in memory and stay fast, since I have enough RAM.
Your distro may vary on the policies around this, but the way the stack generally works, is that whatever shared libraries, and their memory resources, which haven't been touched in a while, are put into swap. So if you've run a few programs, left them open or whatever, switched the UI to other things and whatever over a period of a day, or even a few hours/minutes (its a knob), then bits of those programs will have gone cold. The local libraries and static things that haven't been executed - in a while - will move into that qualification.
You can tweak this, it is indeed a local-system configuration thing that a Linux user should know and think about - and maybe - if your distro has such a thing as a policy about it - this ought to be considered as well.
For example, I use Ubuntu Studio, precisely because it is generally well set up, swap-wise, to absolutely not interfere with audio performance (note: this DAW has been rock solid for decades) .. but every distro has its own 'medium ground' to consider when setting this.
The thesis/antithesis for/against swap is un-useful. Swap is a knob, not a switch.
Maybe not what's happening in your case, but stuff that is in swap will not be moved back to memory unless there's a need for it, so if you did have memory pressure at time 1, which resolved at time 2, then you might still see a lot of swap in use at time 3.
I suspect the intent is to move stale things out of RAM so it can be immediately used for more productive purposes when needed.
Shuffling out 8GB with a spinning hard drive could take a full minute…
That said, I agree with you. I’d rather not use swap unless absolutely necessary and I don’t want the latter to happen either…
Plus, I don’t want my storage in the “critical path” of memory accesses, even if rare.
Use swap for the important things: the part of your operating system at run time which doesn't get used much, but needs to be around when needed - the contents of /lib and /usr/lib and so on, all the *.so's and the things they allocate - shared libraries among the distro install, these things can be tidily swapped, until you need it.
If you need an application to always be responsive, start it with the swappable flag turned off, i.e. something like this:
Anyway, this is a distro-originated knob which every user must tweak for their use-case.
> I suspect the intent is to move stale things out of RAM so it can be immediately used for more productive purposes when needed.
These are separate problems, though. You can clean a page by updating its backing store without purging it from memory; and purging a clean page doesn’t require a write. It’s definitely reasonable to take advantage of idle IO resources to clean dirty pages, but pages should only be purged (clean pages first, usually) when there’s an immediate need for them for something else.
Is this considering RAM for disk cache too?