Ubuntu – swap partition vs swap file

hibernateswap

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.

Best Answer

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 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 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