Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It seems to me a swap file is more flexible.

share|improve this question
up vote 24 down vote accepted

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 is also slightly slower.

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.

share|improve this answer
in the past there was also a performance benefit. But I think that ended when 2.6 came out. – xenoterracide Sep 18 '10 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 '10 at 0:23
@gabe: I was discussing swap files in comparison with partitions. Hopefully this is clearer now. – Gilles Oct 17 '10 at 0:50
I point here my comments in Penz answer. The perceived fs induced performance loss on swap files is moot. – ata Sep 26 '12 at 16:07
Are there any benchmarks proving that swapfiles are slower nowadays? Especially when you're using virtual machines, and all the drives are virtual anyway. – CMCDragonkai May 31 '14 at 5:57

A swap partition is preferred because it avoids the overhead of the file system when all you need is an addressable 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

share|improve this answer
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' – Michael Mrozek Oct 17 '10 at 2:29
this is good information, but it doesn't answer the question. – lesmana Oct 19 '10 at 0:00
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 '12 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 '12 at 19:00
Very useful answer and comments, thank you! – Jeff Burdges Apr 8 '15 at 16:44

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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