No one is claiming “Less RAM is better”. Memory is orders of magnitude faster than disks and having more is (almost) always better. No one (except maybe OP in his naivité) is seriously debating that. But this statement is not correct:
The argument is: A system with SWAP can (in many scenarios) perform better than an identical system (same hardware, same set of workloads) without SWAP
Because: A System without SWAP is by definition unable to reclaim valuable RAM allocated to idle applications, thereby wasting it instead of being able to use it for something more performance critical (at that point in time).
A better way to say things might be: Using SWAP will always be slower than just getting more RAM, but that’s not really an option everyone has…
Say I have a Factorio Server and a Database on the same Machine and both are up to accept incoming connections, but otherwise idle (Factorio pauses the Simulation if nobody is connected, the database also doesn’t do much without incoming queries).
If I now start to hammer the database the DBMS might significanly benefit from having more memory available for processing and caching. And even if the process itself doesn’t allocate more RAM having frequently read file blocks already in memory can also significantly improve performance.
If the Factorio server is idle, it still has a bunch of stuff loaded and is using several GB of memory to respond to and resume running on incoming connections. Whether that’s optimal and I agree with that decision or not, I can’t change it (without halting the server).
Without SWAP the OS has 3 options:
- Kill one of the processes (undesirable, only option if memory is critically low)
- Give less new RAM to the other process and deny (some) requests to allocate more (risky, application might not handle failing malloc() gracefully)
- Drop file caches (in I/O heavy tasks like a database this leads to disk thrashing where the same group of files are read in for a few I/O operations and immediately evicted for some other file, that’s then evicted again to load the first, etc., etc.)
With SWAP there’s a 4th way: SWAP out inactive memory from processes not currently using it. Yes it has to be loaded back in if (not when) it ever needs to be used again, but by then the overall system state might be drastically different and the memory pressure might have passed.
This can be true even if the System has several GB of “available” memory. Sometimes having your entire working dataset in RAM and being able to quickly access more of it (even if only through the block cache) is more valuable than keeping that forgotten browser window on another desktop actively loaded.
It can cause a latency spike on initial access after being swapped out. But unlike killing the process it is mostly transparent and the minutes of time saved by using that memory more optimally (like for the database when it was beneficial) can and does more than make up for it (in many situations).
We drop disk caches when there’s better things to to with that RAM, despite many Applications blocking on I/O. There’s no reason not to “drop (swap out)” inactive application memory if there’s something better (like another app’s allocation or extremely frequently used files) it could be used for.