40

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.

  • About the related question: askubuntu.com/questions/903414/… (if it ever gets answered) – muru Apr 13 '17 at 15:27
  • digitalocean.com/community/tutorials/… - I can't say it made a difference, I really can't. System is working fine as before (the swap was rarely used anyway). – user589808 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
  • 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
28

The only 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.

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 write important filesystems, when you could just use a swapfile 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 swapfile, 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 yet (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
  • 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.... – taifwa Apr 14 '17 at 7:48
  • 4
    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 7: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 8: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. – taifwa Apr 14 '17 at 9:39
  • 2
    @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 2:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.