Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

It seems to me a swap file is more flexible.

share|improve this question
add comment

3 Answers

up vote 23 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
1  
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
1  
@gabe: I was discussing swap files in comparison with partitions. Hopefully this is clearer now. –  Gilles Oct 17 '10 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 '12 at 16:07
add comment

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
4  
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
3  
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
add comment

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
add comment

Your Answer

 
discard

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.