13

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?

  • 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 3: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 & Linux or Ask Different 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 8:44
11

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.

  • Looks like this is true, and swapspace doesn't even dynamically resize swap. – Tom Mercer Apr 17 '17 at 18:11
2

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.

  • 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 9: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 9:21
  • I still vote your answer up as it is just as simple to create new space. – user231695 Apr 17 '17 at 9: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
1

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
  • 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 2 '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 3 '18 at 19:14
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.