I’ve heard a lot of people say your swap should be 2x RAM… but do I really need 32GB of swap?
As most have pointed, the “always 2x” rule doesn’t have that much of relevance in 2023 as most computers now has more than 4GB of RAM. I would only use that much of a swap when using a low hardware.
For desktop, I would never go swapless, though. In the event of memory pressure, swap would still help in that situation so that OOM Killer do not kick off and unintentionally kill my working process. Plus it helps that Linux can move the least used data to the swap and use the RAM for filesystem cache.
So my rule of thumb, for desktop: If RAM < 8GB: Swap == 2x RAM If RAM => 8GB: Swap == 1x RAM
For servers, I think it depends on the workload. I keep a small amount, like probably 50% of RAM or less. But for stuff like Redis, it doesn’t make sense to have swap. You want to ensure that everything is in the memory.
This was pretty much on point.
I would add not using swap for hypervisors and virtual servers as that can cause noisy neighbor issues and tend to increase iowait pressure.
Yea my server is running 128GB of RAM, no way I’m making 256GB of swap.
The 2× recommendation is indeed way old, it stems from a time when computers had 1 or 2 GB of RAM or even less. Nowadays, if you have 16GB of RAM I’d say you’re not going to need it (in most use cases).
Chances are your RAM will rarely get so full that your system will need to swap to disk, it’s probably going to clear buffer/cached data first. This is data kept in RAM that’s not actively used by the system but might be useful soon.
If your RAM does run full you either have some very specific application that demands it (then you probably already know the importance and hopefully wouldn’t ask internet randos) or you have a memory leak - that’s a problem and I don’t believe swapping helps in this case. It’s way too slow for that.
If you run VMs and reserve RAM for each those considerations might change.
Personally I don’t think swap partitions are particularly useful any more, certainly not 2× your RAM. If you ever want to suspend your system, then it needs to store all your RAM content to disk and it will use your swap for it, so 1× your RAM would be required. But with modern systems and SSDs booting only takes seconds, so I don’t think suspend to disk has much utility.
For me swap files are a good compromise. But if a system with 16GB starts swapping, something is not going well.
Even if you don’t run into RAM exhaustion, it is always good to have some swap space, as it can be used to move less used pages to swap, and frees up RAM for more filesystem cache.
I always keep a 2GB swap partition.
right, swap usage is controlled by the swappiness value. With the default value of 60 out of 100 and almost never filling up my 32GB RAM, my swap of 8GB is always used at least a little bit, and depending on the uptime it eventually gets “full”.
Personally I don’t think swap partitions are particularly useful any more
This is just wrong. Having swap increases the performance of your applications as it frees up RAM by swapping cold pages out of memory and use that space for caching, which increases hit rates. And this performance increase is both for low and high used memory rates.
So once it gets pushed out of RAM, you read it from disk instead of from disk? I’m not sure that that’s a benefit. If you’re using SSDs, they are fast and don’t have seek times, so I don’t think that having it in swap is any faster than just from disk. Maybe a little overhead for opening the file.
Assuming that you aren’t truly exhausting your RAM, anyway, such that programs start crashing due to being unable to allocate. Or that the data on disk is compressed, for example.
the swap space works as another level of memory hierarchy, the kernel swap pages out before all RAM is used in order to make room for pages that are more likely to be requested. It is very much a benefit, especially when using SSDs.
so I don’t think that having it in swap is any faster than just from disk.
That is still is faster, because even though they have the same physical access times, the data stored in swap space is already mapped to memory pages. By not using swap you are just more likely to face lower hit rates for low memory usage and thrashing for high memory usage. The slowness of the disk won’t affect your overall performance as you’ll get higher cache hit rates.
Right now for example, roughly half of my RAM is being used for caching and all of my swap is used. If I didn’t have swap, most of that space used for caching would instead be used for those cold memory pages.
I have a swap partition in my current system (64GB), I’ve had one on my previous system (which was 11 years old, with 16 GB) - I have seen my system¹ swap at most 5 times, and every time it was software misbehaving so badly that it made the system unusable.
I know this is anecdotal evidence - but my experience is that it’s getting less and less relevant for most desktop systems.
Edit: ¹my old system, the new one has not swapped at all in the past year.
I have 16gb, I play Overwatch 2 through Bottles (Wine) and my ram usage & swap partition are almost always full when playing.
I setup Swapspace in case I ever need to compile something very big, but usually my system doesn’t use swap.
I usually just have a swap of like 4 or 8 gigs on disk and use ZRAM as well. I set the priority so it uses ZRAM first and if that fills up then it’ll use the swap partition. I don’t think it has ever used the swap partition tho
I suggest that you read “In defence of swap” that various people have linked to. It includes information about swap size.
Here are swap size recommendations for from Red Hat and Canonical. You may not run oof these distributions but the information will probably still apply.
https://help.ubuntu.com/community/SwapFaq
Assuming you have 16GB of RAM 32GB is the maximum swap size you need if want to use hibernation. You can you less if don’t plan to use hibernation.
If it’s a laptop and you want to be able to hibernate, swap must be large enough to hold system memory, plus a little extra just in case. Other than that, everything depends on the workload. Generally, no. Maybe a few gb in most cases.
I think this might answer your question: https://chrisdown.name/2018/01/02/in-defence-of-swap.html (especially the tunning section)
I use gentoo, so my swap is pretty large because it often compiles from source. I have it at 16 GB for my raspberry pi and 32 GB for my pc. I think I’ll be bumping up the raspberry pi to 32
Don’t even bother with a SWAP partition. Create an empty file on your / partition so you can grow/shrink it as needed.
did if=/dev/zero of=/SWAP bs=1024m count=4 mkswap /SWAP swapon /SWAP
Not a golden rule. For example btrfs has limited support for swapfiles.