To Swap or not to Swap and how Swappy should the Swap be?

I just watched this from @EricAdams and I thought he brought up some interesting comments about setting up a swapfile and illustrating the advantages over a swap partition. I have always used the swap partition and once read that it had better performance but Eric brings up a great point that if you need better performance, you are Linux-ing wrong (my words) and should upgrade your memory. In my case, my thought was for hibernation purposes but that also seems like a moot point now too and @argrubbs brought up ANOTHER good point in the comments that you could even use a LVM to manage it. Personally, since I had space to burn, I set up a 17 GiB swap partition on the drive that has my root filesystem and my home partition is the entirety of a separate drive.

Ultimately, it really made me think… am I Linux-ing right?

Side note, this laptop has over 2 TiB of storage on it so 16 GiB didn’t seem like a big deal.

2 Likes

So after spending a little more time researching, here’s what I came up with for swap partition vs. swap file.

Swap performance relies on the swapped data being contiguous on the disk. Swap partitions are inherently contiguous because they are entirely dedicated to swap whereas a swap file may not be contiguous as it is just a file on a general-use partition that shares space with all the other files contained there. This is particularly true for filesystems that have been in use for a while where at least some fragmentation is likely. Disk space is also a factor. If there isn’t a contiguous block of space large enough to accommodate the size of the swap file then it could be split into pieces which will absolutely decrease performance of the swap process. This should not be an issue when creating a swap file on a fresh partition with adequate free space available. In cases where the swap file is contiguous there should be no discernible difference in performance between a swap partition and a swap file.

The other reason I came across was the idea of planning ahead and separating concerns. If you know you need or want to use swap and know how much you want to allocate then dedicating a partition for that purpose makes sense. This is particularly true if you are doing so with the assumption that you will be relying on swap on a regular basis and not as a safety net as I assume most people do. It can be particularly important for mission critical systems like servers. Lastly, the idea that a swap partition can be located anywhere such as a different, faster disk could play a role, although there’s no reason a swap file also couldn’t be located on any mounted disk.

Hibernation was also something I came across. I mentioned it in the video and I genuinely do believe that most people don’t use hibernation at this point but I may be incorrect. You can use hibernation with both swap partitions and files but using a swap file requires additional steps to configure and will vary depending on your approach and distribution.

My final thoughts on this come down to how you approach swap. As I’ve mentioned, I really only see it as a safety net for the extremely rare occasions where I do something dumb and go OOM or the unlikely event some process goes off the rails and eats all my RAM. I will never rely on swap as a primary means of managing memory in a purposeful way where I know I will use all the physical memory and overflow into swap space. It’s a pretty terrible experience in case you’ve never dealt with it. You probably have as a Windows user back in the day where it was very common to have a low spec system without sufficient RAM to run the OS and many applications at once. This combined with the slow disks available at the time was almost unbearable. Remember memory managers? They would push things out of physical memory to free up space, usually didn’t work very well and generally caused more problems than they attempted to solve.

I guess the bottom line is, to me at least, if you are running out of memory on a regular basis…get more. Swap is a band-aid and will not solve that problem. It will stop your system from crashing and hence is still useful so definitely make sure you have thought about it but don’t see it as a way to solve the fundamental issue of too little memory.

2 Likes

The only system of which I do have that does lean on the Swap space is this machine here:

I have since upgraded it to 2 GiB, the max it can handle but it does dip into swap regularly. The reality of the matter is, this is not for me but for my kids to learn and bang away on and whether or not I used Swapfile or swap partition, I think doesn’t matter at all. I will say that SSD made a HUGE difference in performance.

What I think is most interesting out of your research is that the difference between swapfile and swap partition is essentially negligible unless you hibernate (which I have done). I guess, in the end, my reasoning and justification I will give myself on my swap partition size is the hibernation feature that I rarely use. :slight_smile:

Thank you Eric for presenting great information and forcing me to do some learning today! It was Swaperific!

2 Likes

Yes indeed. Think of those hybrid disks that have a solid state cache combined with a spinning disk. It’s kinda sorta like an implementation of swap specific to those disks. It doesn’t solve the problem but mitigates it in 99% of cases (large file operations or seldom-accessed files come to mind).

2 Likes

When I was first installing distros, I read that some programs still expect some form of swap, otherwise some weirdness could ensue if you don’t have one. So, I’ve always created swap partitions on install, even though I’ve got plenty of ram and haven’t (yet) tried to run 3 concurrent VMs like Eric. Is this still true? It adds to the confusion when different distros have differing recommendations and default settings.

1 Like

I haven’t heard that before but I can see that as being a requirement still. I imagine it is possible that some programs could possibly write immediately to Swap but that would be real odd for an application to dictate memory management. Possible… but unlikely.

Here is a good article on setting swappiness.