3

Could having both a swap file and swap partition cause errors? Should I keep one over the other?

Zanna
  • 70,465
Toby
  • 59

2 Answers2

3

It's a common practice to have a partition and dynamically increase swap via swap files. It is an accepted method among others on How to increase swap space? and in fact I have implemented a script to add swap files to make the steps even easier. In fact, keeping original partition and adding/removing a swap file is far easier and safer than increasing partition size, since during partitioning improper steps can lead to loss of data on the main partition, where as swap file concerns only with one file and does not touch partitioning layout or other data located on hard drive.

Errors with having both swap file and partition can result from improper formatting of swap file (think data corruption, file truncated or overwritten by privileged process), if it has been deleted ( unlinked ) or moved but corresponding entry in /etc/fstab has not been updated (if you use /etc/fstab to mount the swap file on boot). However, these aren't directly related to having both swap file and partition, and as far as swap file it is easy to deal with such issues. When it comes to failing hardware, hardware issues aren't directly related to having partition or file, and there's also no good alternative aside from replacing the hardware as soon as possible. It is often recommended that you only use swap file on SSDs, since SSDs have shorter life compared HDDs. But in my research I've so far found no evidence that having a partition on SSD dramatically decreases SSD lifespan and there's other factors to consider.

If you have large amount of RAM (above 4 GB), it might be advisable to keep either small partition (1 GB) or swap file on SSD, since it would be a waste of disk space. ( Note: there's couple other low level hardware considerations which need to be addressed, but I currently cannot recover the sources needed for such discussion)

See also Waren Young's answer on swap reliability.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
2

There will be no errors in you use them both. I personally use partition as a first swap device and file as second.

You can find many religious discussions advocating one over another. Partition should be a little faster as it does not have filesystem overhead, but file is much more flexible.

marosg
  • 1,303