is there a rationale when to create a dedicated swap partition vs. creating a swap file, for example like /tmp/swap?

In my certain case, memory is kind of low (1GB), disk space on a SSD is pretty huge (256GB). Thus, swapping will happen pretty regularly.

Any comments appreciated!



The main disadvantage that I can see for using swap files as opposed to swap partitions is that it's not possible to have a swap partition get fragmented, whereas it's possible that, upon creation, a swap file on an extant filesystem can get split up on the disk, causing slower access. Also, as mentioned, there are more layers of kernel code to traverse when writing to a file on a filesystem on a disk as swap, as opposed to a dedicated partition with no proper filesystem layer.

  • 2
    Since SSD's don't have a random-read penalty there is very little harm from the fragmented file. – David King Dec 15 '15 at 20:28
  • To be honest, I've been in favor for a swap partition ever since, but hey, as we say over here in Germany, "there is no cost in asking" ;-) – CarstenP Dec 15 '15 at 20:38
  • 1
    I'll often make swap files for temporary use if I foresee a large project coming that may exhaust the configured RAM and swap partitions, but I like to keep it partitioned. That way, I can re-use swap partitions between Linux distributions just as readily as I can reuse home partitions. – DopeGhoti Dec 15 '15 at 20:48

I/O operations on a file have to go through more layers of kernel code (VFS for example), so there's more overhead associated with swapping to a file. On the other hand it's a more flexible setup and may be good enough for your case.

  • Perhaps I should get myself more familiar on Swap I/O pomp, ducks and circumstances... Isn't there anything about Linux just being nice, slow and simple?? ^^ :D PS: Well, forget about the "slow" thing... ^^ – CarstenP Dec 15 '15 at 20:41

Access to swap files doesn't go through the filesystem ... the kernel knows which disk blocks are swap and accesses them directly. But a non-contiguous file may have fragments in different areas of the disk, and depending on the specific usage, additional seeking may make it slower than a dedicated partition.

OTOH, if you are dealing with a cloud server that has no swap partition and there is no possibility to create one, a swap file may save your application.

  • What is the case if the fs moves/resizes file blocks? I think in the case of the swap files, at least some fs driver interaction should exist. – peterh Sep 27 '17 at 11:59

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.