Is having swap on hard drive or sd card better?
Matthew Harrington
I have a bunch of 16gb normal speed sdhc cards, so if I use them I'm not worried about them getting damaged, but I was wondering: is swap on a 7200rpm 320 gb hard drive that is also the drive ubuntu is installed on better or worse than an sd card with the only use as swap?
44 Answers
If you're unconcerned with damage to your SD cards then it really depends purely on speed. Your 7200rpm internally is likely slower than your SD card's internal read/write speed, but actually getting data to drive will likely be the biggest bottleneck. Unless your SD card reader is very new, the 7200rpm HDD connected with a SATA should be significantly faster.
Your SD card is unlikely to have a decent microcontroller to address spent/damaged memory very efficiently (how SSD's address increased memory failures), so you'll more than likely not notice any failure until you get a catastrophic failure for the SD card.
If you're using anything "mission critical" on this system, then you should definitely not be going with the SD cards.
13The SD cards will perform significantly worse for swap and it may reduce their lifetime.
Flash media is asynchronous in its access speed. Performing many separate read actions is very fast, faster than usual on a hard drive. However, performing many separate small write actions is very slow, often an order of magnitude slower than on a hard drive.
When you swap to disk, your workload will consist of many small write operations - the worst performing workload for it.
(In case you're wondering, modern SSDs get around this issue with very sophisticated firmware trickery, which simpler Flash drives like SD cards don't have).
Flash media has a limited number of write cycles. This limit is high enough that normal consumers don't need to worry about it - it'll usually be in the tens of thousands of writes. However, if you are swapping then it will increase the wear on the drive quite a lot. Probably not enough to observe any problems over a short timespan.
While you can use an SD card for swap (Raspberry Pi does this, for example) it is not ideal. Use your hard drive, as it much faster than an SD card. Also, an SD card will eventually wear out from heavy usage due to the limited number of writes.
2Based on my comment here
The first thing to consider is the use case for the swap drive. Is this swap drive going to be used often because the system has less memory and is used for running many different applications simultaneously?
If yes, then it's definitely better to go with a swap on the main drive, since many writes to the SD will degrade performance, and in cases where the system wrote a swap page to a corrupted SD, the system will crash and need to be restarted with a new SD.
If no, then you need to consider the speed of the SD card controller (and also the speed the SD card itself supports i.e. the class of the SD card). For this case, @thomasrutter's answer is what you should follow.
The second case to consider (I'm adding this, which is not a part of the OPs question but is relevant with today's laptops) is whether the main drive is a SSD drive or a rotational drive.
For SSD drives, I'd always recommend using an SD card for swap (or no swap at all) because the SSDs are still very expensive, and it's financially sound advice (to degrade the SD which is cheap instead of the SSD, which is many times more expensive).
Finally, let's consider the case for a machine with a large enough RAM and an SSD drive. For these machines (laptops/desktops) the main memory would be sufficient for almost all work loads. Therefore, I'd recommend tuning the swapiness to 0. (the swap would only be used for cases where to main memory is fully used up, or for enabling hybrid suspend/hibernate). For these systems, I'd definitely recommend a swapiness of 0 with a SD card as the swap drive. The reason is that each time you suspend, a number of writes are made to the swap, and degrading an SD card is way cheaper than degrading an SSD drive.