It seems to me a swap file is more flexible.

  • 1
    You can use a swap file in Linux. I'm doing so right now on multiple machines. I'm not using anything special though, just ext4 or ext4 on LVM. No encryption or anything complicated. Suspend and hibernate have worked great for me, even on my NVMe machine.
    – Cory T
    Jul 19, 2017 at 18:33

5 Answers 5


A swap file is more flexible but also more fallible than a swap partition. A filesystem error could damage the swap file. A swap file can be a pain for the administrator, since the file can't be moved or deleted. A swap file can't be used for hibernation. A swap file was slightly slower in the past, though the difference is negligible nowadays.

The advantage of a swap file is not having to decide the size in advance. However, under Linux, you still can't resize a swap file online: you have to unregister it, resize, then reregister (or create a different file and remove the old one). So there isn't that much benefit to a swap file under Linux, compared to a swap partition. It's mainly useful when you temporarily need more virtual memory, rather than as a permanent fixture.

  • 4
    in the past there was also a performance benefit. But I think that ended when 2.6 came out. Sep 18, 2010 at 18:30
  • It seems like this doesn't really answer the question. Or am I missing something? The answer only talks about swap files, and doesn't mention partitions at all. @Gilles was that intentional, or did you just make a typo?
    – gabe.
    Oct 17, 2010 at 0:23
  • 1
    @gabe: I was discussing swap files in comparison with partitions. Hopefully this is clearer now. Oct 17, 2010 at 0:50
  • 1
    I point here my comments in Penz answer. The perceived fs induced performance loss on swap files is moot.
    – ata
    Sep 26, 2012 at 16:07
  • 4
    According to Ubuntu Community Help Wiki help.ubuntu.com/community/SwapFaq A swap partition is needed for hibernation. Quote: "It cannot use a swap file on an active file system.". And regarding performance, it seems there is no difference unless the file is sparse. serverfault.com/questions/25653/… Jul 19, 2017 at 9:45

A swap partition can be preferred because it avoids a dependency on the file system when all you need is an addressable memory pool.

But nothing prevents you from using a swap file instead of a swap partition, or in addition to a swap partition.

  • Create the file:

    dd if=/dev/zero of=/extraswap bs=1M count=512
  • Initialize file contents's:

    mkswap /extraswap
  • Use it:

    swapon /extraswap
  • See if it worked:

    free -m

In order to start using the swapfile always at bootup, edit /etc/fstab and add

/extraswap           swap          swap    defaults    0 0

[1] http://www.redhat.com/docs/manuals/linux/RHL-8.0-Manual/custom-guide/s1-swap-adding.html

  • 6
    Useful alias I've been using forever to let me type moreswap in a terminal if I suddenly need extra swap space: alias moreswap='swapfile=`mktemp /tmp/swapXXXX` && sudo touch $swapfile && sudo dd bs=512 count=1M if=/dev/zero | tee $swapfile | pv -s 512M && sudo mkswap $swapfile && sudo swapon $swapfile' Oct 17, 2010 at 2:29
  • this is good information, but it doesn't answer the question.
    – Lesmana
    Oct 19, 2010 at 0:00
  • 6
    Also, the bit on filesystem overhead is incorrect. Contrary to common belief, there's zero overhead on using swapfiles. Andrew Morton gives the simple explanation here. That's one of the reasons using a dynamic swap-file based swap manager like the swapspace daemon is a good solution if well configured.
    – ata
    Sep 26, 2012 at 16:01
  • Let's add that the swap file approach can be your only option when you are running in the cloud.
    – sorin
    Dec 20, 2012 at 19:00

Perhaps the main reason is that the main kernel suspend-to-disk does not work with swap files. For example the Debian wiki instructions are to install uswsusp if you need this.

More recently, swap files do not work if the filesystem is btrfs, so it is simplest for distributions to always create swap as a partition.

It is vaguely mentioned that using a file for swap had potentially lower performance than a partition, prior to kernel version 2.6. https://www.kernel.org/doc/gorman/html/understand/understand014.html#text15


For completeness I'll add my own answer:

As said by @Gilles, a swap file can't be used for hibernation. When using a swap file, the system must locate the swap file's header, but in order to do this the filesystem that contains the swap file must be mounted, and a journaled filesystem - e.g. ext3, ext4, and basically all modern filesystems used by Linux - cannot be mounted during resume from disk. (In reality, there are ways to do so, but quite cumbersome.)

Source: https://www.kernel.org/doc/Documentation/power/swsusp-and-swap-files.txt


I think that it is mainly because the access time to the datas located on a partition are lower. The point of the swap file is more to help the sys admin when he is really out of RAM and needs to operate huge operations that would maybe crash his system. In this case he will sporadically create swap files when needed.

But anyway you can have both of them.

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.