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.) Commented Apr 18, 2017 at 11:28
  • Does Mac support dynamic swap?
    – Tom Mercer
    Commented Apr 19, 2017 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. Commented Apr 19, 2017 at 8:44
  • @DavidFoerster on Windows you can set the minimum and maximum sizes for each pagefile and Windows will increase the pagefile size when necessary. tomshardware.com/news/…
    – phuclv
    Commented Dec 1, 2019 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
    Commented Feb 25, 2021 at 22:51

5 Answers 5


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
    Commented Apr 17, 2017 at 18:11

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
    Commented Apr 17, 2017 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
    Commented Apr 17, 2017 at 9:21
  • I still vote your answer up as it is just as simple to create new space.
    – user231695
    Commented Apr 17, 2017 at 9:26
  • I will give this a try, and report back.
    – Tom Mercer
    Commented Apr 17, 2017 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
    Commented Apr 17, 2017 at 18:13

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
    Commented Nov 30, 2018 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
    – Denix
    Commented Dec 2, 2018 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
    Commented Dec 3, 2018 at 19:14

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.


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.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .