I can't imagine why a swap file needs to be fixed size. Why not let it resize dynamically, like the hard drive image file for a virtual box?
5 Answers
It doesn't look like Linux supports dynamic swap file sizing (at any rate, I couldn't get it to detect that a swap file had changed size without a swapoff/swapon).
Presumably it is easier to assume that a swap file doesn't change file size dynamically when writing code for supporting it. I don't see any great use for it either. Since you can use multiple swap files, nothing's preventing you from creating more swap files as needed.
Also note this paragraph from the swapon manpage:
The swap file implementation in the kernel expects to be able to write
to the file directly, without the assistance of the filesystem. This
is a problem on preallocated files (e.g. fallocate(1)) on filesystems
like XFS or ext4, and on copy-on-write filesystems like btrfs.
I'd think the same problem would apply to a dynamically-sized swap file.
- 207,228
SwapSpace is a utility that will allow you to make dynamically sized swap if that is what you are looking for. I do believe it is available in Ubuntu.
- 207,228
- 69
- 2
I know it is late, but I think the best solution for dynamic swap is to install dphys-swapfile :
sudo apt install dphys-swapfile
sudo update-rc.d dphys-swapfile enable
then setting CONF_SWAPFACTOR=2 in /etc/dphys-swapfile and finally
sudo service dphys-swapfile start
I quote the dphys-swapfile site :
This project has produced an dphys-swapfile script for sizing, setting up and mounting an swap file. And an init.d script that runs this script at boot and shutdown time.
Swap space can be added to a running system by using the swapon command. It needs an existing (unused) swapfile/partition. To create one, use the dd command to allocate a contiguous file, and then mkswap to add the correct control information to the file / partition.
To remove a swapfile, use swapoff; the file can then be deleted.
I suspect that is what SwapSpace dæmon that @user231695 mentioned does.
Note that swapoff can be a slow operation if the swapfile is in use; all the data has to be transferred to another swap area.
- 201
- 1
- 2
There appears to be a program named swapspace. Available from Ubuntu 18.04 onward.
The corresponding GitHub page. They recommend using a static swap file if you want to suspend and resume work. They also do not recommend using it for systems that need to be responsive.
People have asked about how to use it. Those details are mentioned in the readme file.
- 1,149
- 13
- 26