Monday, 30 January 2012

Linux Swap File vs Swap Partition


Interesting piece on swap files from Andrew Morton himself.

.. "From a software point of view with the 2.6 Linux kernel, swap files are just as fast[14][15] as swap partitions. The kernel keeps a map of where the swap file exists, and accesses the disk directly, bypassing caching and filesystem overhead.[15]"

Q.) Does creating the swapfile on a journaled filesystem (e.g. ext3 or reiser) incur a significant performance hit?

A.) None at all. The kernel generates a map of swap offset -> disk blocks at swapon time and from then on uses that map to perform swap I/O directly against the underlying disk queue, bypassing all caching, metadata and filesystem code."

The implication is as follows: "With a 2.4.x kernel swap files were slower than swap partitions, but with the 2.6 kernel a swap file is just as fast as a swap partition."

Of course this depends on the following:

1.) That the swapfile is in contiguous space and therefore not fragmented
2.) That you have any need or desire to use a pagefile. Swap partitions work perfectly well.
3.) You're running a 2.6+ Kernel.


  1. Interesting, but not sure I understand why you wouldn't want to use a swap partition. Especially given that filesystems become fragmented which annoys my obsessive compulsive tendencies!

  2. There are a few reasons why you may want to, and a few why you wouldn't :-). You know more than I do about Linux, but here is my rationale.

    1.) Much easier to resize a pagefile than a partition. Okay, LVM offsets this, but it's still a pain.

    2.) If you create the swapfile at the start of your install, as you would a swap partition, then it would be contiguous.

    3.) easy to whack a swap file on any disk that happens to be on the system without having to do partitioning.

    I think the argument goes either way frankly. I suspect however that most people will stick with a swap partition as it's 'standard' and there is no reason why not.

  3. Another reason (becoming more common) is you may have no choice but to use a swap file is when using a virtual private server and your file system resides on a single virtual device. ...Robert

  4. There is "swapspace" which dynamically manages swap files.