Swapfile vs Swap partition vs no swap at all?

Guys,

Swap has been a topic that seems to rely more on opinions rather than hard facts these days. For many years it was considered to be conventional wisdom that using a swap partition was a better than using a swap file. Usage of a swap partition can be somewhat constraining though - particularly for newbs, there is lots of drama regarding setting up your disk, the swap partition getting in the way of filesystem expansion or requiring lots of LVM complexity, etc. Furthermore, time marches on and technological developments make some old ideas no longer valid. MacOs has always used a swapfile since going to the BSD based MacOs X. Ubuntu now ships by default configured for swapfile instead of a swap partition. The ArchWiki now says there is no real difference between using a swap file and a swap partition (by the way I don’t agree with that statement - there are definitely some differences that matter in edge use cases. I would rephrase that to “very little difference for most use cases.”)

Add to that the fact that advances in filesystems such BTRFS or OpenZFS may change the relative merits of swapfile and swapspace, Additionally, more usage of solid state drives vs spinning drives has an effect on this question as well. Finally, technologies like ZRAM may change the debate.

So I was hoping to get some of this wise and friendly group’s wisdom on the use of swap files vs a swap partition or even no swap at all. Not only would it help me, but it might create some valuable discussions on EndeavourOS installer default setups. However, there are a lot of edge cases, so I would like to bound the questions a little bit.

Let’s assume the following:

  • desktop use (vice server use - if setting up a server the answer is likely to be it depends).
  • Solid State Drive
  • Arch or Arch based systems like EndeavourOS
  • Ext4 file system (most popular and easiest to set up) and no encryption.

Given those assumptions, would you use a swapfile or swap partition?

Would the answer change on BTRFS?
Would the answer change if running in a virtual machine instead of raw metal?
What other memory management stuff might you consider, such as use of ZRAM?

My personal thoughts are that at this point in development and under the limited scope above that a swapfile would be best for a hardware machine, and no swap at all for a virtual machine (just control memory and hibernation of the virtual machine at the host OS level using the virtual machine software).
Many of the reasons why swap partitions were preferred in the past seem to have changed over the years, and a swapfile makes for a simpler setup. Obviously there are still cases in which a swap partition would be better than a swap file, but they tend to be more specialized cases.

Just looking for some opinions on this from the smart people in the crowd.

Thanks and Happy New Year.

swap is always a debate like if you got enough ram… but is like a expansion of your heater. you can use swapfile or partionate itself does not mather i think…

i am kinda old fashioned, using a swappartition… but installing without and adding a swapfile is always possible. thats also bit personal reference

A very recent thread:

Some discussion on swap here as well:

1 Like

The obvious missing thing here is…how much memory do you have?

Generally speaking, between a swapfile and swap partition you are trading performance for flexibility. The swap partition is higher performing and the swapfile gives you more flexibility. Practically the performance difference in most desktop workloads is negligible so a swapfile is usually a better option. ZFS is an exception. You should never put swap in any form in a zpool due to this.

If we are limiting things to an SSD, I would always choose some swap over no swap. It is basically the choice between decreased performance and an OOM condition causing crashing. On an HDD, I would use no swap instead(or zram)

That being said, I think zram and/or zswap are better options than a straight swapfile.

2 Likes

This is a good read.

https://chrisdown.name/2018/01/02/in-defence-of-swap.html

2 Likes

I think people create drama where there does not need to be any. The Linux kernel uses swap space to dynamically allocate memory pages regardless of the amount of memory in the computer. The only two questions that affect most people are:

  1. Is my disk space so limited that I cannot spare 2-8Gb?
  2. Do I plan to hibernate the system?
    If you have enough disk space, and do not plan to hibernate, then create a swap partition around 2-8Gb depending on the amount of memory your have and be done with it. You should never need to adjust your swap space (so all this nonsense about swapfiles being more flexible are moot).
    If you plan to hibernate the system, create a swap partition in the amount of physical ram + 2Gb and you will never have to revisit your decision.
    If you ever run out of ram, even for a moment, having the swap partition may just save your bacon. And what is the cost? A few Gb’s of hard drive space? If your hard drive space is so limited, its time to buy a bigger hard drive.
    And I almost forgot, if you find you are using swap space frequently, anything above a few megabytes that the kernel will sometimes do, then it is time to buy more ram. :sunglasses:
3 Likes

I hibernate therefore I swap. I prefer a file over our partition.

Thank you I had read some of those posts before they had so many responses. That’s what caused all this research and further questions.

Hibernate removes all doubt - you must have swap. Most people recommend a partition for hibernate, but it is possible to do with a file, as you are doing it. Have you had any problems with the hibernation using a swapfile?

1 Like

Great inputs here. Regarding memory, it’s my philosophy that you don’t want a lot of swap going on, of the hits to performance just get to be too bad to tolerate. Therefore, I would be looking at “adequate” memory. So EndeavousOS claims 1 GB memory minimum, with 2GB recommended, so let’s consider 4 GB for a more than adequate memory size to avoid constant thrashing.

With this philosophy of “adequate memory” I am looking at swap more for other benefits, such as putting some rarely used memory pages in swap for efficiency or having an emergency capability in case I or the system does something stupid and creates an out of memory scenario. Obviously if you want to hibernate you must have swap so then the question becomes how much. How much is fairly straight forward and well discussed all over the internet - the subtly becomes file vs partition.

From your comments, it seems like for the use case I am proposing (typical desktop use), the performance is likely to not even be noticeable. That would suggest that swapfile would be better, as it is more flexible than a partition.You still get whatever benefits you might get from swap, but with less drama for disk partitioning, installs, etc.

You mentioned not putting swap in a zpool using ZFS. Do you have any experience using swap with BTRFS?

Yes, in fact I did read that wonderful link before writing my post. The author would argue that swap is still necessary despite having adequate memory. So my “no swap” query seems to be answered if folks concur with the post’s author’s experience. I am wondering whether people in practice have tried no swap options and whether they caused significant issues or not?

If you opt out having a swap file/partition then having some amount of zram is a viable alternative for those occasions kernels feel like to swap some pages. A zswap, if I remember correctly, works in conjunction with a swap device but zram will work regardless. Here is some read about zram from Gentoo wiki. I found the compression rate especially interesting.

https://wiki.gentoo.org/wiki/Zram

For how to set up zram, if you choose to do so, Arch wiki got it covered.

Thanks for this great post. In my case, I am not limited on disk space so much, it is a mater of choosing sane defaults for the average user. For example, I find that Linux noobs are put off by firing up an installer and having to answer these questions as they are installing without ever having used the distro. I find that EndeavourOS is a great way to ease people into the Arch community without so steep a learning curve as one would need to install Arch the Arch way. But things like deciding on your swap scheme before even using the distro can be asking too much of someone trying to upgrade their game from a more beginner Linux distort like Elementary or Mint. Bottom line, if most people just need a small swap file vice a separate partition, why complicate the setup and install and lose those people before they even try the distro. I still think that swapfiles are more flexible - I’ve seen some distro installers that position the swap partition at the end of your partition table. That’s fine for a hardware install, but for a virtual install with a dynamic disk image you can’t grow the filesystem easy - you either need to reformat your virtual disk and move the swap partition or do some more LVM voodoo to create another physical drive and move that into your virtual root volume. All doable but very intimidating for a newbie. Hopefully not enough to send them back to Windows or Mac but maybe enough to send them to a different Linux distro with a more user friendly set up.

Much of my question also comes from virtual machine usage. Since swap in a virtual machine is writing virtual memory pages to a virtual Hard Disk Image, it seems fairly point less, Better to increase the virtual processor memory instead of relying on swap.

For hibernation, swap is absolutely required, so there is no question of that.

By the way, thanks for posting the info about your own distro spins. I’m looking forward to trying them.

Yes, since kernel 5.0, btrfs supports swapfiles but they require special handling. You need to create a separate subvolume for your swapfile with snapshots disabled and set some flags to disable copy on write.

More info on that can be found here.

I have run many no swap systems over the years. There are no problems if you don’t run out of RAM. If you do, it is mess. That being said, these days there are better options of oom management so I think it would be better than it was then but at the end of the day, if you run our of memory, you run out of memory. Applications getting killed to keep your system afloat is better than the alternative but it still sucks.

Workload plays a big part here. For most of my desktop workloads, I wouldn’t be able to guarantee that I could stay under 4GB. I would probably use zram in that scenario.

I hope you enjoy the personal Arch and Debian spins I do. Thanks for the post on swap. I realize I am old school and my opinions were formed in the Neanderthal Period of Linux, but I do prefer swap partitions and I always place them at the front of the disk. I despise auto-partitioning for my hardware installs, but in videos on VB, they are quite handy. I would always suggest a real new user start with virtual machine installs to test and learn. The best way to learn is to mess up and realize the mistake, so VM’s are great for that.

1 Like

I chose not to include any swap on my desktop the last time I installed Arch. With 16 Gig ram, no hibernation, and no ram intensive tasks, I haven’t had any issues so far. I use a swap partition for my EOS work laptop since hibernation is occasionally necessary.

Thanks I do plan to try zram regardless of how swap discussions play out. Thanks for the point out to the gentoo page - I’ve read about Zram but hadn’t read that page yet.

1 Like

I agree that if you are going to use a swap partition that putting it after boot and before root is the way to go. I have an old install of something Debian based from ages ago that has the swap at the end of the disk. Easier to get rid of it that way but more complicated to expand root. I think the reason some people choose to put it at the end is so if they add more RAM and want to add more swap, having it at the end of the disk instead of the front might be easier. Great discussion, thank you.

Just some feedback on all the good info provided here:

systemd-swap is awesome (https://github.com/Nefelim4ag/systemd-swap#about-configuration). I installed it, configured the dynamic swapfile management by uncommenting

swapfc_enabled=1

in the swap.conf file and let it rip. Zswap is enabled by default, no need to do anything (some tutorials say it is disabled by default - that was true a few years ago, now the kernel enables it by default). No need to use Zram since Zswap is enabled and performs a similar but not identical function as Zram.

The dynamic swap controlled by systemd-swap co-exists fine with a traditional swap partition - you can play around with it without changing your existing set-up. It will just make the swap partition a lower priority than your dynamic swapFC chunks and likely never get used. You can use swapoff -a to turn off all other swap files or partitions and systemd-swap will still work, creating dynamic swapfiles as you need it. You can use htop to watch it grow, and

systemd-swap status

for more detailed information. Awesome little tool.

So if you aren’t hibernating, and you have an SSD so having a contiguous part of spinning hard drive to speed up disk access isn’t important, based on a few days of playing with this, I would definitely recommend not running a classic static swapfile or swap partition and using swapfc instead. Obviously there are high performance use cases in which you would want a dedicated file or partition, but for most casual desktop use, assuming you have adequate memory, this systemd-swap service just rocks.

(My only problem was that I can’t seem to get normal swap to go away. It always comes back after re-boot, despite my attempts to mask the systemd unit in accordance with the Arch Wiki. When I try to make or disable the unit it says it doesn’t exist - probably is a syntax problem, but I am copying the unit description right from systemctl output. It’s not a big deal, because it doesn’t get used, but I would like to figure this out so I can get rid of the swap partition. I suppose if I just wipe the swap partition out, systemd wont autodetect it and enable the traditional swap partition.)

Thanks again to everyone for your hints and links.

1 Like

There’s a warning at the start recommending to use zram-generator instead, is this what you are doing?