71

I was reading the new changes with Ubuntu 17.04 and one caught my eye which was moving from the typical swap partition model to a new swap file model for new installs.

Are there benefits for using this as opposed to a swap partition, maybe related to or in terms of performance, space saving, more in line with today's hardware like SSD and NVMe or something else?

Another related question is how can I migrate from a swap partition to a swap file? I'm guessing using dd but want to confirm since I did not know about the swap file scheme until now.

And lastly, will hibernation be an issue with swap files?

Of course these questions might be related to cases where the user has a lot of RAM and the swap is rarely used (I'm guessing). So just to confirm that this information will help me and others with the same doubts.

wjandrea
  • 14,236
  • 4
  • 48
  • 98
Luis Alvarado
  • 211,503
  • About the related question: https://askubuntu.com/questions/903414/upgraded-to-17-04-how-do-i-destroy-my-swap-partition-and-live-17-04-to-the-full (if it ever gets answered) – muru Apr 13 '17 at 15:27
  • https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04 - I can't say it made a difference, I really can't. System is working fine as before (the swap was rarely used anyway). –  Apr 13 '17 at 15:29
  • Here's how to add a swap on file. In 17.04 it is a file /swapfile. Hibernating may be an issue. – Takkat Apr 13 '17 at 15:51
  • muru @CelticWarrior Takkat thank you for the links and information guys. Very helpful indeed. If possible Celtic, if you could please add the specific answers here in AskUbuntu just in case. For everyone, making sure the answers are up to date (not 2012, but actually 2016-2017, when the whole swap file thing got started) would help a lot and avoid anything that happened during that time period that has changed. – Luis Alvarado Apr 13 '17 at 16:27
  • Good idea - updated my answer. – Takkat Apr 13 '17 at 18:05
  • I'm afraid, this question falls under too broad category – Anwar Apr 14 '17 at 08:16
  • 1
    If you have the root partition encrypted, then swap file is encrypted as well -- automatically. No need to do anything special. So it is an improvement for general user, who may forget to encrypt swap. – Maris B. Apr 17 '17 at 10:59
  • I got hibernation working with some extra tweaks (but my machine is a basketcase anyway) – Zanna May 15 '17 at 07:23
  • @Maris B. Yesterday, I did a fresh installation of Ubuntu 18.04 with a swap partition and and the installer complained the swap partition was un-encrypted and didn't want me to proceed. I wasn't aware of the change to a swap file until then. – Daniel Jan 14 '19 at 20:14
  • both are the same, and is worse than zram. So it's better to use zram nowadays and keep a small whatever of swap file or partition in lower priority – phuclv Jan 05 '20 at 09:10

1 Answers1

57

The primary advantage of a swapfile is that it is easy to resize, so there isn't much point in transitioning unless you are unhappy with your swap partition size. You could move the swap to an encrypted partition for security, but there are other ways of encrypting your swap.

Traditionally swapfile were discouraged for a number of reasons. The swapfile used to be slower before Linux v2.4, and might still be slower if you create the swapfile on a fragmented filesystem. You may be a bit more likely to hit memory bugs with swapfiles, for example the catch-22s where you can't allocate any more memory until you swap some out, but the filesystem needs to allocate before you can swap out. A few years back hibernating to swapfiles was a controversial new feature for similar reasons. Years ago when filesystems were still a bit buggy (and not journaled) it was unwise to do huge numbers of writes to important filesystems, when you could just use a swap partition instead. As the tradition default, swap partitions have been tested more than swapfiles. Presumably Canonical think that these problems aren't worth worrying about anymore.

The biggest reason now not to switch to a swapfile, is "why fix something that isn't broken". If you don't backup your main partition, and accidentally delete your /home instead of your swap partition, it would be a hassle trying to get it back.

Another reason not to switch to swapfiles, is if you are using btrfs which doesn't support swapfiles before kernel 5.0 (except via slow loopback files)


Even if you do decide to go for a swap file, there is no real need to delete your swap partition unless you are short on space. You can use both at the same time if you want. If you do decide to delete the swap partition, first of all boot with a Ubuntu LiveCD, and go try without installing. Then in a terminal run gparted, delete the swap partition, and resize the remaining partitions (doing a backup first may be a good idea).

Now reboot into your regular Ubuntu install and create the swapfile. Since the advantage of a swapfile is that it is easy to resize, you may want to use SwapSpace to manage the size of your swapfile:

sudo apt install swapspace

You could also manually create a swapfile by following the instructions at: https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04

To cut a long story short, you can make a 1G swapfile by pasting the following into a terminal

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show

If that works, you can make it permanent by doing:

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
gmatht
  • 741
  • 6
  • 6
  • Hmmm citations needed.... not sure about the buggy filesystems argument how far back are you going...? As for catch 22 the oomkiller would have sorted that out to a degree - small allocation should have caused small kill, and i would be surprised if swapping via fs did not have a wee buffer at the ready for such an eventuality.... – tk-noodle Apr 14 '17 at 07:48
  • 8
    10+ years ago, the general advice was to put swap, /usr /home and /var all on diffierent partitions so that if one of them corrupted themselves you could recover from the others. That was probably before journaling was common. Filesystems still have bugs, but paranoia is probably better spent on making backups in places ransomware can't get to it. – gmatht Apr 14 '17 at 07:53
  • 1
    @taifwa I have added a link to btrfs FAQ where they don't support fast swapfile access (because they aren't sure how to implement it without corruption), and a note that the biggest reason not to switch is not to fix what isn't broken. Does this help? – gmatht Apr 14 '17 at 08:51
  • Good points then, yes it is of help. On re-reading, yes I would second your advice to not change an existing set up if it is not broken. As for new installs, I'd say it probably won't make much difference these days. Filesystems will be buggy in the sense that every software likely has bugs; but I don't think, at this level of discourse and considerations, any of it matters enough... As you point out, Canonical probably believe that such bugs will only arise in corner cases. – tk-noodle Apr 14 '17 at 09:39
  • Also a good catch on btrfs - I would presume this would remain true of any snapshottable filesystem in general - I'm thinking ZFS but also wondering about LVM ... in the latter's case, I would have thought a logical volume dedicated to swap would make more sense than a swap file... – tk-noodle Apr 14 '17 at 09:40
  • 1
    @gmatht Well you covered everything there in a very nice way. If you find any new information in the following months / years to come with swap files, let us know by updating this answer. Kudos and excellent work. – Luis Alvarado Apr 14 '17 at 15:06
  • BTW, this isn't specific to swapfiles, but OOM killer can cause a race condition when the fs needs to allocate: https://lwn.net/Articles/627419/ – gmatht Apr 14 '17 at 17:15
  • how do one disable the swapfile to use a swap partition only? – yinon Apr 20 '17 at 02:39
  • I read here: https://help.ubuntu.com/community/SwapFaq I hope it will help – yinon Apr 20 '17 at 02:46
  • There is no performance advantage to either a contiguous swap file or a partition, both are treated the same way., found at https://wiki.archlinux.org/index.php/swap – mook765 May 14 '17 at 16:20
  • @mook765 tweaked by discussion. – gmatht May 15 '17 at 13:12
  • Beside the performance aspect, does SSD lifetime get greatly reduced if you have a swap partition with a lot of writes and maybe sometimes with little free space? – Pablo Bianchi May 30 '17 at 02:52
  • 4
    @PabloBianchi It can do, but I would prefer swap on SSD these days. Putting swap on a rotational disk can increase wear too. Modern SSDs tend not to wear out much faster than rotational disks. – gmatht Jun 11 '17 at 02:38
  • @gmatht - That depends on whether you actually hit Swap. If you are frequently using swap it can have an impact on SSD life: More so than HDD. – ravery Aug 22 '18 at 21:28
  • Since kernel 5.0 btrfs support swap files. Would be great some words about swap files making life easier if you want everything encrypted (beside ecryptfs deprecation and LUKS) – Pablo Bianchi Dec 30 '19 at 18:12
  • @PabloBianchi Do you know of an advantage of btrfs encryption of swap over LUKS? – gmatht Jan 05 '20 at 08:41
  • For Ubuntu 14.04: sudo swapon --summary. – jw_ Jan 22 '20 at 06:04