Remix.run Logo
j_w 2 days ago

Swap is really interesting. The idea of swap sounds super useful - you get more RAM than you have. The reality is that RAM speeds are so important to fast computing that once you start to need swap, what you really need is more RAM on your machine.

If somebody told me they were running into issues with swap on linux I would ask them why they don't just get more RAM. I'm currently running 32GB and have never used swap on this machine. That includes gaming and local LLM usage (which my GPU does not have enough VRAM for, so normal RAM gets involved).

baq 2 days ago | parent | next [-]

Swap is not 'super slow extra memory' (well it is, but that's not useful!), it's a place to temporarily store contents of memory for things less important than what wants to be in fast memory right now. Idle apps can (and are) swapped out to have more file cache even on boxes with large RAM. Memory can also be fragmented and you'll have nominally lots of free space, but in reality allocations will fail; in this case, swap can be used to defragment memory instead of OOM killing stuff.

You always want some swap, even if it's 1GB for a 96GB machine.

godelski 2 days ago | parent | prev [-]

I think you really are misunderstanding what swap is for.

If your speeds matter, you, and I cannot stress this enough BUY MORE RAM[0]. It's called "swap" not "RAM" so anyone trying to tell you it is "more RAM" is lying to you or woefully naive. It would be as idiotic as having no swap space.

Swap is a cache. Swap will actually help your RAM be even faster! Go look at your RAM usage in a bit more detail. You get a little from using a tool like `htop` but you'll get more from just `cat /proc/meminfo` or `free -wh`. You RAM has tiers of memory inside of it, all RAM is not equal. You should see that some is compressed and a lot is cached. (`/proc/meminfo` will show you there's a whole lot more to this than just "RAM and swap")

I'll put it this way. My machine has 64G of RAM on it and ~9G of swap. Currently the system (rebooted yesterday) is using about 8G of RAM and 200M of swap. Except that's actually a lie, that's what htop tells us. In fact, we need to check from `free`. Of my 62Gi of RAM: 8Gi is used, 7Gi is free, 1Gi is shared, 3Gi is in buffers, 45Gi is cached and 54Gi is available. (Swap is identical: 200M) When my system is running longer, that swap isn't so minimal anymore. Things get paged into it despite having tons of RAM available. This isn't because the OS is dumb, it is because the OS is smart.

The only reason to not have swap is because you really really care about a trivial amount of disk space. But man, disk is cheaper than RAM and these days you're probably using NVMe or at least an SSD.

What you should do:

  - Follow the instructions from [1] or elsewhere[2] and get yourself at least 4G of swap but I would do 8. Are you really going to miss 8G of disk space?
  - Change the swappiness value[2]. Set it to 10 to get pretty similar results to what you have but without crashes.
  - Read more about what swap files actually are because you are currently giving a strong impression that you have vastly oversimplified how a computer's memory system works.

[0] https://news.ycombinator.com/item?id=45008336

[1] https://help.ubuntu.com/community/SwapFaq

[2] https://docs.redhat.com/en/documentation/red_hat_enterprise_...

[3] https://wiki.archlinux.org/title/Swap#Swappiness

j_w a day ago | parent [-]

I definitely don't misunderstand what swap is for. I've implemented swap for a hobby os.

Swap is absolutely "more RAM" in layman's terms. If you NEED 10GB of memory to have open everything you want on your machine, but you only have 8GB of RAM, swap will make that happen for you. Now ideally the OS is using this for inactive pages (those programs you have open but you aren't actually using), and the nuance to how swap can be used to make the RAM you do have more effective is an interesting attribute of swap (another commenter mentioned swap being used as temporary storage to defrag the physical memory), but every single reference you linked says the same thing - swap is for when you need more memory than you have physical memory for.

godelski 9 hours ago | parent [-]

  > I definitely don't misunderstand what swap is for. 

  > Swap is absolutely "more RAM" in layman's terms. 
Okay