11

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!

Carsten

1
4

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 extent filesystem can get split up on the disk, causing slower sequential access.

4
  • 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
  • 1
    The part about “more layers of kernel code to traverse when writing to a file on a filesystem” is not true: “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.” – Linux-Kernel Archive: Re: Swap partition vs swap file – Anthony Geoghegan Mar 10 '20 at 11:01
3

Access to swap files doesn't go through the filesystem ... the kernel knows which disk blocks are swap and accesses them directly1. 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.


[1]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.” – Linux-Kernel Archive: Re: Swap partition vs swap file

2
  • 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
  • Upvoted. This should be the accepted answer as it's the only one that states that the kernel directly accesses the disk blocks used for swap. – Anthony Geoghegan Mar 10 '20 at 11:04
1

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.

2
  • 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
  • That's not actually true: “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.” – Linux-Kernel Archive: Re: Swap partition vs swap file – Anthony Geoghegan Mar 10 '20 at 10:58

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.