20

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?

Tom Mercer
  • 1,658
  • 3
  • 17
  • 36
  • Linux doesn't support dynamic swap space sizes unless you count the option to add and remove entire swap spaces at run time. (Neither does Windows btw., not even the latter.) – David Foerster Apr 18 '17 at 11:28
  • Does Mac support dynamic swap? – Tom Mercer Apr 19 '17 at 03:39
  • I have no idea. It's an entirely different kernel which I didn't study. Chances are it's similar to swap space in Linux. Anyway, could you please open a new question [Unix.SE] or [Apple.SE] if you have a new or follow-up question about the Darwin kernel of OS X? The comment section is not suitable or meant for new questions or extended discussion. :-) Thanks. – David Foerster Apr 19 '17 at 08:44
  • @DavidFoerster on Windows you can set the minimum and maximum sizes for each pagefile and Windows will increase the pagefile size when necessary. https://www.tomshardware.com/news/how-to-manage-virtual-memory-pagefile-windows-10,36929.html – phuclv Dec 01 '19 at 10:36
  • 1
    Late response, but macOS swaps aggressively (which annoys folks who want to minimize SSD wear) and yes, in my experience it tends to increment the swap files in 1GB increments and will continue doing so indefinitely as far as I can tell. This has the effect of the system getting more and more sluggish but things generally stay running, whereas Linux OOM killer behavior is at the other end of the spectrum. I went from working in a 96GB system to 32GB (temporarily) and I watch memory consumption like a hawk now. Planning to set up a big pagefile to help with this a little. – Steven Lu Feb 25 '21 at 22:51

5 Answers5

12

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.

muru
  • 197,895
  • 55
  • 485
  • 740
5

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.

muru
  • 197,895
  • 55
  • 485
  • 740
  • While it had a new release 2 years ago, the versions packaged for Ubuntu all seem to be ancient. How well does it work? – muru Apr 17 '17 at 09:18
  • I cannot imagine that the method used to write the swap file has changed but I could not say for sure. Works fine on debian. – user231695 Apr 17 '17 at 09:21
  • I still vote your answer up as it is just as simple to create new space. – user231695 Apr 17 '17 at 09:26
  • I will give this a try, and report back. – Tom Mercer Apr 17 '17 at 18:12
  • @user231695 while it's simple to create a new swap space, it's horrendously inconvenient, and possibly completely unworkable as a solution. The only time a user wants to add a new swap space is the very moment when existing swap is full. Hence my question for dynamic sizing of swap file. – Tom Mercer Apr 17 '17 at 18:13
  • This doesn't appear to dynamically size a swap file, but, rather, to create new swap files as needed. We'll see if that works nicely. – Tom Mercer Apr 17 '17 at 18:16
  • @TomMercer: See also swapd, another daemon to dynamically create more swap files on the fly when running low on swap. On filesystems where fallocate can create usable swap files without actually writing zeros (I think only ext4, definitely not XFS), it can work well without needing to do a lot of I/O when the system is already thrashing. That page also mentions a stalled rewrite of swapd that hooks into the kernel. – Peter Cordes Jul 04 '17 at 06:54
2

I know it is late, but I think 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
  • 269
  • 2
  • 6
  • It doesn't appear to dynamically resize swap. Looks like a script to initialize a swapfile of certain size that defaults to 2x RAM capacity. – Tom Mercer Nov 30 '18 at 19:09
  • Hi @TomMercer setting swapfactor and maxswap, according to comments in the file: "# set size to computed value, this times RAM size, dynamically adapts, guarantees that there is enough swap without wasting disk space on excess" . "dynamically adapts" seems what you are looking for. Best – Denis Pitzalis Dec 02 '18 at 18:54
  • Does it make my swapfile vary in size between, say, 1KB and 16GB, while I'm using the OS and filling up RAM? That's what "dynamically resizing" swap means. Creating a swapfile that's a multiple of RAM size is not dynamic. There's no point in having a swapfile at all until RAM is full, and then you want the swapfile to be exactly the size of VRAM you're using. If you close the tabs or huge image you're working on, the swapfile should dynamically resize back down. Swapfile should always be exactly 100% full by dynamically resizing itself. – Tom Mercer Dec 03 '18 at 19:14
1

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.

CSM
  • 201
0

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.

Nav
  • 1,059
  • 1
    That's not new. 2 answers mention it. Does it work now, or does it still fail?

    https://askubuntu.com/a/905723/605513

    – Tom Mercer Jun 05 '22 at 18:56