My question on Swap

Is it necessary to have swap file. Just heard the most recent episode on Destination Linux and they are saying it is a must. Going on our Wiki I found a post by @Bryanpwo stating that if you are using an SSD it’s not a good idea. Now I do have the capability to add an HDD to this laptop as well. Please school me on Swap and would adding the HDD be a good idea?

It depends on the criteria of each one and the way to install a system in the sense of how much ram memory you have for the swap file, the less ram, it is better to have swap.
You could start by reading the wiki, if you haven’t already.

https://wiki.archlinux.org/index.php/Swap

2 Likes

I you want to use swap, you should definitely put it on your SSD if you have one. Faster underlying IO will make a big difference to swap performance. The problem with swap is that there is no perfect solution or right answer it depends very much on your specific workload. Some information about swap.

  • Running out of memory will cause your machine to fail
  • If you don’t run out of memory, you don’t need swap
  • Trying to supplement a small amount of memory by regularly using swap will cause severe performance degradation
  • Ironically, machines with large amounts of memory get the greatest benefit from swap if they have workloads capable of utilizing all that memory.
  • The concerns with SSDs are typically about wear and overall SSD life. Most modern SSDs have wear leveling algorithms that should handle this for you. However, if you are worried about it, you can use a swap file instead of a swap partition.

I have gone back and forth on swap over the years. When RAM was much smaller, we always used swap. Later on, as RAM become cheaper, I stopped using swap because the performance penalty was so high. These days, access to inexpensive, fast SSDs makes swap more viable again.

For desktop workloads, I always have some type of swap available but typically not a lot. For me, it there so if something goes wrong I have a chance to recover, not to be used as an extension to RAM. If you aren’t using any kind of OOM killer, I think swap is worth having.

Of course, there have been advancements in swap technology as well, things like zram and zswap are interesting options as well.

7 Likes

@Resiliencia @dalto thank you guys. That answers that.

I would also like to point out that the hibernate feature uses swap to save the content of RAM. So there’s definitely need of swap for hibernate.

To be safe that would be a 16GB swap. But with some settings you can minimize the hibernation file size by using compression, keeping in mind that makes resuming from that file a bit slower. Now, if you don’t use the hibernate feature then swap is not necessary at all.

You could use a file based swap, which would allow for a dynaimcally-sized swap, without the need to provision a certain amount of space for it.

You might want to look into systemd-swap for that.

2 Likes

As @nate noted, if you want to use hibernate, then yes you need it. If you don’t care to use it, it’s totally up to you.

If it’s a low RAM computer, or you find you are the type that needs A LOT of power, you should use it.

If you have a 16gb RAM machine and you only ever use 4, and you never have your computer hibernate then it’s totally up to you if you’d rather have the HDD space or the swap space. It’s not necessary.

Also if you do intend to hibernate, swap should be equal to or greater than RAM.

1 Like

The thing about swap space is, if you are using it regularly, you should install more memory. Having swap space is like have insurance, you don’t really need it until you need it. Also, I point people to this article: https://chrisdown.name/2018/01/02/in-defence-of-swap.html - excellent explanation of swap and need for it even if you think the system does not need it. Having some swap space allows the kernel to better optimize memory usage no matter how much memory you have. Don’t worry about SSDs and swap, the wear leveling makes sure the drive is evenly utilized. There is no real difference between a swap partition and a swap file, they both work fine. I prefer a swap partition of 4Gb and I never have to think about changing it.

4 Likes

Truth from the horse’s mouth :slight_smile:
Seems like that guy knows what he’s talking about.

1 Like

Yes, if hibernation is mentioned, the swap size should be equal to the RAM, …although in the late Antergos days I had a pretty fast but small disk and 8 GB of RAM on the machine with it. I set the swap partition below half the size of RAM and checked the hibernation. Not once in almost 2 years there was no problem. Arch wiki says something about it.

It is probably because memory is compressed into swap for hibernation so as long as your actual RAM+swap usage at the time of hibernation doesn’t exceed your available swap after compression, it should work.