• 4am@lemmy.zip
      link
      fedilink
      arrow-up
      7
      ·
      edit-2
      18 hours ago

      Not necessarily no. That’s how it used to work a long time ago but swap is also used to store infrequently accessed memory in order to help free up contiguous blocks. Even if your memory isn’t 100% full, if a program needs a large sequential block and can’t get it, this can also cause an out of memory error (one large block in physical memory is faster than trying to break it apart using virtual addressing to spread it across gaps - although I think modern OSes can do both but prefer the faster solution). Also, modern OSes LOVE to cache tons of stuff in RAM whenever they can and keep it there (unused RAM is wasted RAM), but they can’t act like memory is full if they are doing that! It would drive users nuts. So that cache can just be thrown away and then reloaded later when something more important needs RAM. However, what happens if you have say a file cached and it hasn’t been written to the proper disk location yet and you need to free up space? You can swap out the cache until you are ready to write it to its proper disk location.

      Swap isn’t just “overflow RAM” anymore like how Virtual Memory was in O.G. Mac System 7, it’s got a lot of complex uses now in order to make sure that the user can get the most out of the computer, instead of getting errors and having to close things when all it would take to keep things smooth is a little re-arranging.

      This is also why it’s a bad idea to “just turn off swap” for container hosts and K8 nodes - it’s not always the performance gain you think it will be (depends highly on your workload of course!) and you should instead be tuning it rather than disabling it!