Reasons to choose swap files over swap partitions

An enduring legacy of UNIX is the paradigm of partitioning a disk into several partitions for various purposes. While this might still be appropriate in certain scenarios I think it would be wise for modern GNU/Linux operating systems, especially when used on a desktop, to move toward a single physical partition model. The default for Ubuntu when not using LVM is to create 2 partitions: a root partition and a swap partition. I think the default should be 1 partition for root and that swap should be handled by a file on the root partition instead of on a dedicated partition.

The primary argument for using a swap partition is that it provides for better performance. I am willing to concede that this is true but I question whether the performance difference is enough to justify the rigidness and additional complexity of having a fixed partition for swap. I would argue that at the point you have a system relying on swap to the degree that the performance difference between a file and a partition are evident you have far more serious problems to content with. A system that performs well will rely on swap rarely, and hence the performance differences will no be noticeable. Furthermore, according to Andrew Morton, the performance differences between file and swap were largely eliminated in the 2.6 kernel (Re: Swap partition vs swap file).

And for choosing a file based swap what you may lose in performance you gain in flexibility. You can alter the size of the swap at any time which you cannot do easily with a partition. This also means the disk will be better utilized. On my Kubuntu desktop the installer chose a 6GB swap partition for my 2GB RAM system. Of that only a few megabytes are ever in use thereby wasting a substantial amount of disk space. If I were to do it over again I would have a swap file or probably 512MB and if that was ever insufficient I would simply expand it at that point.

The additional complexity of partition based swap especially makes itself known in systems with a RAID setup. Even on a RAID 1 system with 2 disks being mirrored the root and swap partitioning scheme means creating 2 MD devices, one for each partition. On a swap file based system there would only need to be 1 MD device and it would enjoy the benefits of being completely redundant and able to survive 1 disk failure.

Upon reading Creating dynamic swap space I was exposed to the dphys-swapfile package which, once installed, creates a file swap with a default size of twice the amount of RAM. This is a very simple way to help people implement a swap file without having to use the command line at all, which makes it suitable for distributions. Of course, many have argued that twice the RAM size is no longer a useful guideline, and I would have to agree, because the reality is the system would become unusable long before that amount of swap was used. But dphys-swapfile makes it easy to configure the amount of memory in the file /etc/dphys-swapfile. I would suggest that such a file could be written in such a way to create appropriate swap file sizes for computers with small amounts of RAM (where twice the RAM size is appropriate) as well as those with large amounts of RAM (where half, or a quarter of the RAM size might be appropriate). Or, another thought is to have a swap file size ceiling, perhaps of 1GB for example.

The only reason I would not choose to use dphys-swapfile is because I prefer my swap to be encrypted for security purposes, both on the desktop and the server. It would be nice to have dphys-swapfile be updated to easily configure an encrypted swap file but the setup process is fairly trivial. Alternatively it would be nice for a GNU/Linux distribution such as Ubuntu embrace a change from swap partitions to swap files and handle such a setup, including the option of encryption, in the installer. A GUI front-end to swap settings would be nice too, and it’s something that Windows has had for a long time. Ubuntu could have a good set of defaults and recommendations as well as the option of encryption in a way that makes it easy for users to change their settings based on their needs.

Creative Commons License Except where otherwise noted, content on this site is licensed under a Creative Commons by-nc-sa 3.0 License