by RetroTechie · 2y ago · 0 comments
If an OS+apps' working set exceeds RAM, performance will slow to a crawl in any case.
Best (and only?) argument I've seen for having swap, is that in a gradually building out-of-memory situation (say, a memory leak), swap buys the admin more time to respond / diagnose the problem.
That is a valid reason. Use swap where it applies.
But for eg. desktop systems: pointless. Some app eating all the RAM? Crash right now, please! Then it's usually obvious which app was the culprit. As opposed to slowing everything to a crawl slowly over time & leaving the user to wonder what's going on.
As an example: I run a small Hetzner cloud instance (for about $4.50/month) for quick prototyping when I am away from home. It has a nowadays-considered-wimpy 2GB of RAM. It also exposes a Jupyter server that I (or friends) can use for quick experiments from anywhere in the world.
Some time ago I added torch and torchvision modules for a quick size comparison on a couple of models (just number of trainable parameters) and "pip install torch" got OOM-killed before I realized that I have zero swap and added it (and could have deleted it after the install).
This is not a common example, but a general observation that while using swap in the production servers, running one thing in a highly predictable fashion (uhm, sure) might mask the problem you want to expose, swap is extremely useful for general-purpose computers that occasionally run things that cause memory use spikes and are not particularly sensitive on whether they take a few milliseconds or a few seconds. My 2c.
But for the swap to work efficiently, you still need to have enough RAM to run the things you want to run right now. The slowing to a crawl occurs when the running apps try to touch pages all over the place, requiring very frequent swapping in/out. Then yes, it's time to terminate something and free up some space.
That sounds like a misunderstanding how memory is allocated and this may be the root of it. There are many instances where this would be completely expected. The most trivial perhaps being an application that mmap() a file much larger than memory. That is a perfectly valid thing to do.
> Some app eating all the RAM? crash right now, please?
It's ambiguous which is using "all the RAM." If you have game in one window and browser in another, and you start seeing high memory pressure, which should crash? Or if you're building software and you've fanned out to dozens of processes building dependencies concurrently but all using very little memory independently, which ones should be OOM-killed?
Alternatively you can see processes that haven't been woken or taken focus in awhile and page their memory out to swap to release main memory for applications that need it, and avoid crashing or losing data.
Swap is also used by the system's hibernate function to store a copy of RAM to disk.
If you have 64GB of RAM and no combination of your apps, cache, etc will ever approach that, then probably nothing will ever swap. But for more limited systems or when running apps with higher memory pressure, swap does increase system performance.
No swap just means you think everything ever allocated should always stay in ram. In pretty much every system there's going to be a few GB that were allocated but never really needed. I'd rather that not be true but since it is I'd like that stuff paged out to give the os more file cache.
4 and 8 gb ram seem still most common.