Black Lives Matter

Swap on SSD done right

TL;DR : If you have SSD and need swap, you should use swapspace daemon for healthier SSD. Btw, you want SSD and you need swap.

Note: I expect you to be familiar with RAM, swap and concept of paging.

Here is the short edition:
RAM is fast memory for currently running programs.
Swap is kinda like backup RAM. In case of RAM is filling up, swap will be uyilized to move less often accessed data off the RAM.
Paging is moving data between RAM and swap memory. It occurs when RAM is close to full and needs emptying into the swap or when a data in swap is needed in RAM.

Since swap is located on HDD or SSD, it is much slower than RAM and we want to utilize RAM as much as we can. But sometimes RAM fills up and computer runs into OOM state. Even though it is slow, swap can be a life (or work) saver in such cases.

Estimated Memory Speeds

Device Type Speed (MB/s)
HDD 100
SSD - SATA 560
SSD - NVMe 2000-3000
RAM DDR2 3000-8000
RAM DDR3 8000-12000
RAM DDR4 19000-35000

Data of RAM speeds is from this article of Crucial. HDD and SSD speeds are commonly observed speeds. Not from the Crucial's testing.

You definitely want swap memory to avoid possible OOM issue. You also may want to use SSD for swap memory since it is much faster than HDD or your computer only has SSD. But SSDs have one weak spot. Storage blocks of the SSD that is being read and wrote 100s of times will fail. Thankfully SSDs got smarter. Instead of using a single block too often and causing it to die early, SSDs tend to utilize every block equally. This is called Garbage Collection. And together with overprovisioning, they increase longevity of the SSD.

I don't think fixed swap partitions and files are good for SSD's health. We need a system to dynamically allocate swap. So that swap file will be erased when notneeded and created in lesser degraded blocks when needed.

Meet Swapspace

Swapspace is a background service that understands swap needs and either creates or removes swap files on the fly.

Swapspace is available on many local repos, AUR and on GitHub.

Note: How aggressively the Kernel should use swap memory can be controlled via a value called swappiness. Read about changing swappiness for better RAM utilization.

Day 12 of #100DaysToOffload


Toot on this thread to comment. This blog is a static site. Comments won't appear here.