Can Windows and Ubuntu share a single swap partition?

Solution 1:

It's possible, but probably only as an experiment:

Note: This is a what-if answer and is not recommended, especially if you are using hibernate from Ubuntu!

  • In theory, yes, Windows should be able to use your swap partition for its page (swap) file with some clever scripting on the Linux side.

  • I'm not going to do a step-by-step because I haven't tried it and if you can't figure out how to implement this you shouldn't be trying it either :)

    1. Format the partition as NTFS from within Windows and set up Windows to use that drive exclusively for its swap file (nothing on C:)

    2. Have a startup/upstart script to format the partition as Linux swap on Ubuntu startup and enable swap (swapon).

    3. Have a shutdown script to swapoff and reformat the partition as ntfs.

Alternative (still not recommended) option:

You can create a swap file by following this guide on the NTFS partition, provided the NTFS partition is large enough for both swap files.

However this alternative is not recommended because:

  • Swapping on an NTFS(3G) partition would be extremely CPU-heavy.
  • And to make things worse, since the file can/will be fragmented, that would also cause a performance hit.

Solution 2:

If your goal is to save hard drive space -- not having to waste many GBs for both Linux's swap and Windows's pagefile, then you might consider using dynamic swapfile size on linux via SwapSpace:

  • https://unix.stackexchange.com/questions/134258/dynamically-growing-swap-file-on-debian
  • http://www.hecticgeek.com/2012/01/automatic-virtual-swap-space-creator-ubuntu-linux/

Solution 3:

I haven't tried it myself but you can mount a swap partition with swapfs. You can then, in theory, place your pagefile there (there's some good reasons not to, such as minidumps, but its an option).