-9

Swap doesn't make sense at all unless you are very low on ram.

  • disc is 1000000 x slower than RAM
  • swap makes everything wherefor much slower

It should not be activated at all unless in very last moment when there is no RAM. Why? Because RAM is million times faster.

  • I have also seen that you can park swap into GPU memory. Is this good change? Shouldn't gpu memory be only for graphics like hard drive in reality is for long term memory. Maybe this can overheat gpu? – wxvbnlz Jun 12 '23 at 07:59
  • I can argue with you forever that swap is technically "low memory emergency solution". This increases system load and temperature. Therefor you should start to use swap only in emergency situation and use hardware for what it was designed. – wxvbnlz Jun 12 '23 at 08:01
  • I disabled swap and plan to use it only for emergency situations. Everything is cool and faster this way. :) – wxvbnlz Jun 12 '23 at 08:02
  • This is like you are arguing that PC hardware is wrong. Like some parts of typewriter are technically wrong and manufacturers don't know how to make them. Even though both PC and typewriter appear to work without swap. – wxvbnlz Jun 12 '23 at 08:06
  • I see argument "swap makes everything faster". What if you boot with 0% swap usage? Only RAM is used. – wxvbnlz Aug 24 '23 at 20:39
  • I get it. You don't see that swap is made into "RAM graveyard" point of view. You think that "this is RAM emergency". IMHO it's not. – wxvbnlz Aug 24 '23 at 20:45
  • Or swap can be criticized as processor or ram. Less you use either of them cooler both stay. As swap is hard drive intensive this obviously means increase in operating temperature. – wxvbnlz Aug 26 '23 at 13:55

3 Answers3

5

Swap doesn't make everything slower; indeed it can make things faster.

See, some applications have pages loaded into memory that are never used; they're there 'cos of how the app was built, included libraries and assorted complications, memory allocations etc, but the executable path never uses it.

So why not throw those memory pages out to swap and free them up for other use? Now we have more RAM available for things that need it? Even if that usage is disk I/O cache, by pushing stuff to swap that isn't used we get better use of RAM.

Now we know there's no "one size fits all" solution to this, which is why modern systems allow for tuning; we can go from "only use swap when we run out of RAM" to "opportunistically put stuff in swap". Different use cases may benefit from different tuning parameters. And there are even use cases where zero swap is the best solution.

If you want swap to only be used when RAM is used up then the Linux "swappiness" setting can help you.

  • 1
    So why not throw those memory pages out to swap and free them up for other use? Because reading back pages from SWAP to RAM introduces lags and latency spikes and generally makes the system behavior erratic. The kernel has not freaking clue which applications are worth swapping out and which must remain in RAM at all times and in absolute most cases you get a situation when an important application has been swapped out, you switch to it and WAIT. That never happens without SWAP. – Artem S. Tashkinov Jun 05 '23 at 23:30
  • 2
    The kernel doesn't swap out applications. That went away in the 90s. Old style swapping (eg SunOS 4) is not what modern systems do. Now we swap out pages. And the kernel does have a pretty good idea of what memory pages are used. So it can throw away pages from an application that is running without impacting that application 'cos the app doesn't use those pages. – Stephen Harris Jun 06 '23 at 00:59
  • 1
    The kernel doesn't swap out applications. Now we swap out pages. Does it matter? Applications code, no matter what it is or how it is called, is swapped out. Here's an example how beautiful SWAP is: https://unix.stackexchange.com/questions/747850/ubuntu-becomes-slow-after-huge-file-move-during-deep-learning Show me a single recent scientific study how SWAP speeds up anything, I dare you. People continue to talk about SWAP as if we are in 1995. No, you don't need SWAP = 2 * RAM. In fact if you've got enough RAM, SWAP is just not necessary if not outright detrimental. – Artem S. Tashkinov Jun 06 '23 at 01:00
  • 2
    @ArtemS.Tashkinov Not having any swap means you are wasting RAM on code/data pages that are never used when it would be better to swap that garbage out and use that RAM on something useful (code/data that IS actually being used and disk caching). Having swap available is never detrimental - the kernel does not just randomly swap stuff out for no reason, it swaps unused/infrequently-used stuff out when something else needs memory - so either you have enough RAM and nothing much is ever swapped, or you are being given an irrefutable signal that you need more RAM in the system. – cas Jun 07 '23 at 05:11
  • 2
    swap these days is not a long-term substitute for RAM - it is for avoiding out-of-memory conditions (which can cause application or even kernel crashes) for short-term temporary situations, so the machine and applications can keep running (with reduced performance) until that temporary situation is over, rather than being killed. In short: swapping is not a problem. Swap thrashing is a problem, it's a sign that you need more RAM. – cas Jun 07 '23 at 05:20
  • @cas you are wasting RAM on code/data pages that are never used when it would be better to swap that garbage out and use that RAM on something useful The problem is, the kernel swaps out code pages for filesystem buffers which is in absolute most cases very bad and does slow down your system later on. You really haven't thought it through. Again, studies! Again, proofs! I'm so freaking tired of people repeating SWAP based inanities/myths for 20 years now. – Artem S. Tashkinov Jun 07 '23 at 07:42
  • Having swap available is never detrimental - having SWAP if you have enough RAM is almost always detrimental. You do not want to swap out running applications ever. Again, show me proofs. – Artem S. Tashkinov Jun 07 '23 at 07:44
  • 3
    @ArtemS.Tashkinov you've been banging this drum for several years now. Your opinion about swapping is utterly wrong - as I and others have pointed out to you before. You obviously do not care to learn or correct your mistakes so I do not care to waste my time repeating how you are wrong over and over again. – cas Jun 07 '23 at 11:31
  • as I and others have pointed out the onus of proving that SWAP increases performance is on solely you. You've showed nothing for your words. You obviously do not care to learn or correct your mistake - and that's ad hominem. Neither today, nor previously have you demonstrated that SWAP is benefitial for system performance when there's enough RAM thus I will continue to dismiss your "arguments" with prejudice. Saying that "I and others" doesn't mean anything to me. Sorry, I just don't buy into BS even if it's backed by masses. Masses are too often wrong. – Artem S. Tashkinov Jun 07 '23 at 14:00
  • If you're so correct, there must be an easy way to demonstrate that, right? Go head create a reproducible test case and prove your PoS with hard numbers. Otherwise that's just air. And remember: https://en.wikipedia.org/wiki/Burden_of_proof_(philosophy) – Artem S. Tashkinov Jun 07 '23 at 14:03
4

Swap doesn't make sense at all unless you are very low on ram.

This is only true if the system runs a 100% predictable workload - e.g. a server/VM that is only running one specific application where what it deals with is fully known and planned.

Let's say you are running a Linux desktop computer, however - you're working on a large video file, decide to take a break, and watch some Netflix in your browser for a while. But then you get bored of Netflix and decide to play a game.

Performance for your game will be better if the video editor gets paged out to swap.

Because RAM is fast, it's best to use that for things that are actually doing user-facing stuff - and that's going to be somewhat unpredictable when an actual human user is actually at the wheel. The computer cannot read the user's mind, but it can make some educated guesses - if application X isn't doing anything and hasn't received user input or focus in Y minutes, put it in swap so things that are receiving user input or doing work can have the RAM.

LawrenceC
  • 10,992
  • Comments have been moved to chat; please do not continue the discussion here. Before posting a comment below this one, please review the purposes of comments. Comments that do not request clarification or suggest improvements usually belong as an answer, on [meta], or in [chat]. Comments continuing discussion may be removed. – Jeff Schaller Jun 07 '23 at 10:35
3

Swap doesn't make the system slower. Running programs that try to use more memory than you have ram makes the system slower. If those programs weren't trying to use so much ram, the swap wouldn't be used and it wouldn't be slower.

Or worse, try running the same programs without swap. Now the system stops dead when it runs out of memory. Or something gets killed by the OOM reaper. Is it faster now without swap?

Now, if you get an ill behaved program that sees "Oh look 100G of swap and 20G of ram, let's use 90G of memory just because it is there" where it wouldn't do that if it was more constrained, then the system will be slower with more swap. The solution isn't to use less swap. The solution is to constrain that program either with tuning parameters or by putting it in a memory constrained container that allows you to specifically limit the program's swap use or overall memory use.

The original theory behind swap is that (for instance) the swap device might be 10x slower than memory, so you want roughly 9 out of 10 of your pages to come out of ram and 1 out of 10 to come from swap, and then it about breaks even. In some cases, the system might even be able to predict it is going to need that swapped page and might start reading it early so it is in ram by the time it is needed and then the cost is zeroed out as long as your ratio is good. Alternately, if you have an application with a memory leak where it never uses those pages again, then swap gives you more memory for free.

The point of swap is that your swap device might be 10x or 100x slower than ram, but it is also 10x or 100x cheaper than ram...so you can pretend you have more ram, and trade off cost for time. If you've got the money and the ram slots, by all means, buy more ram and stop using swap!

Our computers today are based on the Turing machine model. But an ideal Turing machine has an infinitely long tape (memory) and our computers have finite memory. Swap is just one way to get us a teeny bit closer to that ideal model.

user10489
  • 6,740
  • Swap doesn't make the system slower. Too often it does make the system slower. Or worse, try running the same programs without swap. Exactly how I've been running all my PCs for two decades now with zero issues. And over 100 high load servers. Here's an example how it makes the system crawl to a halt: https://unix.stackexchange.com/questions/747850/ubuntu-becomes-slow-after-huge-file-move-during-deep-learning – Artem S. Tashkinov Jun 06 '23 at 00:58
  • Now I took manual control over FAN speed and connected it to 5V USB port. FAN just spins always now as result my PC is suddenly very cool. – wxvbnlz Aug 24 '23 at 20:42
  • I have taken 2 emergency actions: 1) manual control over swap 2) manual control over FAN. Now I have instead of burning PC which plays snake with memory graveyard on hard drive watercooled PlayStation or at least it seems so. :)) – wxvbnlz Aug 24 '23 at 20:43