16

I know how to create a swap file and use it as swap. But I have to configure the size of the file beforehand and the space is used on the disk, if the swap is used or not.

How do I create a swap that has an initial size of 0 and grows on demand?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
rubo77
  • 27,777
  • 43
  • 130
  • 199
  • If swap space is to grow on demand then presumably there must be disk space available for it to grow. In that case the disk space can't have been used. At what point does disk space switch from being unused, and therefore wasted in a "good way" to allocated to swap, and therefore wasted in a "bad way"? – roaima May 04 '20 at 07:18
  • It would be "wasted" if you have swap partition that is never fully used – rubo77 May 04 '20 at 07:21

3 Answers3

17

SwapSpace is a utility that creates a ‘dynamic swap file’ which according to the requirements of the operating system changes its size.

So you can even forget about creating a virtual swap file and just install “SwapSpace” and it’ll automatically create one for you and will even resize it when necessary.

You can also use ‘Swapspace’ side by side with a manually created swap file and when the manually one gets filled ‘Swapspace’ will automatically create another one for the OS so the OS will always have a swap space and it enhances the stability.

Another useful thing about “SwapSpace” is that, whenever it can, it’ll reduce the size of the SwapSpace and “release” those bytes into user file system and according to the developers this reduction helps to increase the swap file’s performance as well (plus your “precious” HDD space is not wasted too).

Install with

sudo apt-get install swapspace

And adjust the config file to your needs. I set the minimum to 0 on my VM debian machine with just 8GB HDD


Additionally I also set the swappiness to 0 to minimize the usage of the precious HDD space on my VM:

To change the system swappiness value, open /etc/sysctl.conf as root. Then, change or add this line to the file:

vm.swappiness = 0

Run

systemctl restart systemd-sysctl.service --system

to activate

rubo77
  • 27,777
  • 43
  • 130
  • 199
  • This won't support swsusp/suspending to swap file, unless the size of the created swap file is already big enough to contain the RAM dump. – soze Aug 26 '15 at 02:37
  • So how could we make sure that the swap file gets big enough to contain the ram? – rubo77 Nov 29 '15 at 18:21
  • 1
    Have one fixed size swap file the size of RAM for suspending, and then use swapspace for dynamically sized swap – Tom Hale Oct 11 '16 at 11:17
  • It worked for me. This is super useful for small SSD disks. – Adrian Lopez Apr 14 '17 at 15:54
  • swapspace config path `/etc/swapspace.conf` – a55 Jul 23 '21 at 08:29
  • Did not work as expected when tried on Debian9. After `sudo systemctl start swapspace.service` no new swapfiles were added even after all memory was consumed. – watbywbarif Mar 23 '22 at 09:01
6

Swapspace is old and unmaintained and could lead, one day, to problems in modern systems. I think that the best solution for dynamic swap is to:

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
Denis Pitzalis
  • 393
  • 3
  • 6
  • This works, but keep in mind, that you could be better off without swap, see https://unix.stackexchange.com/a/136133/20661 – rubo77 Jul 01 '19 at 22:53
  • 2
    This doesn't dynamically managae swap, it just adds swap once when the service starts – w00t May 04 '20 at 04:45
  • 1
    It is not true that it is unmaintained. The oldest source changes are 12 months ago, and latest issue closed was as recently as 2/21. If what w00t says is true, this is the wrong answer. A dynamically sized swapfile is, of course, standard in Windows. – CodeLurker May 10 '21 at 14:41
0

Afaik, any swap partition that you would make using fdisk for example, would need any space greater than 0, as you are modifying the space from an existing disk and reallocating it into somewhere else.

Anyway, and answering your question, you can easily make a small partition, assign it as swap under fdisk, and afterwards:

1) disable swapping with swapoff -v 2) resize partition with lvresize 3) format the partition with mkswap 4) re-enable the swap space with swapon -v

Afterwards, you can use cat /proc/swaps to check if it has been upgraded or not. You can easily use this on a script, depending on your system specs, and run it on demand for whenever you need to increase that swap space. Something like:

$ swapoff -v /dev/swapvol1
$ lvresize /dev/swapvol1 -L +1G
$ mkswap /dev/swapvol1
$ swapon -v /dev/swapvol1
AleksanderKseniya
  • 1,044
  • 7
  • 3
  • 1
    This doesn't answer the question because the swap size is changed manually, not on demand. Furthermore, resizing the LV isn't useful: where do you find the space to grow? how is the freed space on shrinking not wasted? The question asks about a swap file, which solves the issue of the free space — it's available for other files. – Gilles 'SO- stop being evil' Jun 03 '14 at 22:58
  • Changing the swap size on demand is a good idea, but swapspace does not cover the scenario of swsusp being used for supporting hibernation. I believe this answer might be more sane for those users who need hibernation support. Until swapspace handles that better, of course. – soze Aug 26 '15 at 02:40