1. Overview
In this tutorial, we’ll discuss the reasons why using Linux swap partitions on solid-state drives (SSDs) is generally not recommended. We’ll explore the basic concepts of swap partitions, the characteristics of SSDs, and the implications of combining the two. We’ll also examine alternative solutions and best practices for optimizing system performance and longevity.
However, we must note from the outset that if our swap space is unused or used very infrequently, its impact on our SSD is negligible.
2. Characteristics of Swap Partitions
Swap partitions are a type of virtual memory used to extend physical memory (RAM). When RAM is full, the operating system moves inactive memory pages to the swap partition, freeing RAM for active processes. This mechanism prevents system crashes due to insufficient memory and allows for smoother multitasking. However, swap is much slower than RAM, so using it slows down system performance.
We can check our swap usage with the free command. In this example, we have about 1GB of completely unused swap:
$ free -h
total used free shared buff/cache available
Mem: 15Gi 3,3Gi 7,5Gi 573Mi 4,7Gi 11Gi
Swap: 975Mi 0B 975Mi
Swap isn’t just for improving memory. It also stores data from RAM during hibernation, provided the amount of free swap is greater than or equal to the amount of total RAM.
Traditionally, swap partitions were located on hard disk drives (HDDs), which, despite their slower speed compared to RAM, were a cost-effective way to increase system memory. With the advent of SSDs, which offer significantly faster read and write speeds than HDDs, it may seem logical to use swap partitions on SSDs to further improve system performance. However, this approach has significant drawbacks that warrant caution. In particular, each write contributes to the consumption of the SSD’s program/erase (P/E) cycles, reducing its overall lifespan.
SSD wear issues apply to both swap partitions and swap files.
3. Characteristics of SSDs
SSDs use NAND flash memory to store data, which offers several advantages over traditional HDDs. These benefits include faster data access speeds, lower power consumption, and increased durability due to the lack of moving parts. SSDs have revolutionized data storage by providing faster boot times, faster file transfers, and overall improved system responsiveness.
Despite these benefits, SSDs do have certain limitations. One of the primary concerns is their limited write endurance. Each flash cell in an SSD can only withstand a limited number of write and erase cycles before becoming unreliable. However, Linux allows us to easily monitor the health of our SSDs.
Modern SSDs use techniques such as wear leveling to spread write operations evenly across the drive to extend their lifespan. However, intensive write operations can still lead to premature wear and degradation of the drive.
4. The Impact of Swap Partitions on SSDs
Let’s see why heavy use of swap partitions on SSDs presents several challenges, particularly in terms of performance and endurance due to increased write amplification and other factors.
4.1. Increased Write Operations
Write amplification (WA) is a phenomenon in SSDs where the amount of physical data written to the NAND flash memory is greater than the amount of logical data the system intends to write. This occurs because as data is written, modified, or deleted, the SSD must reorganize its memory to maintain performance and longevity, resulting in additional write operations beyond what the host system initially requests.
If the system frequently writes and deletes data in swap space, this results in more frequent garbage collection and wear leveling operations within the SSD. These additional internal write operations cause the SSD to write more data than the logical swap data size, exacerbating write amplification.
4.2. Performance Degradation
Although SSDs are faster than HDDs, they’re still significantly slower than RAM. Heavy reliance on swap partitions can cause noticeable performance degradation as the system constantly accesses swap space on the SSD. This can negate the performance benefits that SSDs typically provide, resulting in slower application response times and an overall less responsive system.
4.3. Thermal Stress and Potential Data Corruption
Intensive write operations can generate heat, which, over prolonged periods, can contribute to thermal stress on the SSD. While SSDs can handle heat, continuous high-temperature conditions can affect their reliability and longevity. Ensuring adequate cooling for systems that heavily use swap on SSDs becomes an additional concern.
Moreover, as SSDs approach their write endurance limits, the risk of data corruption increases. If an SSD fails or becomes unreliable, data in the swap space could be lost or corrupted, potentially leading to system instability or crashes.
5. Best Practices and Alternatives
Given the drawbacks of using swap partitions on SSDs, several best practices and alternative approaches can help us mitigate these issues and optimize system performance.
5.1. Adequate RAM Allocation
One of the most effective ways to reduce reliance on swap partitions is to ensure that our system has enough RAM for our intended workloads. Increasing RAM is always a more performance-enhancing solution than relying on swap space. This reduces the need for the system to offload memory to the SSD, preserving its lifespan and maintaining performance.
However, there are alternative strategies to effectively manage RAM usage without necessarily increasing physical RAM:
- Avoid multitasking abuse → Running too many applications at the same time can lead to excessive RAM usage
- Limit browser tabs → Opening dozens of browser tabs can consume a lot of memory
- Manage virtual machines → If we’re using hypervisors, we should allocate RAM wisely and avoid overcommitting resources.
- Optimize software → We can use a lightweight Linux distribution, desktop environment, programs, and scripts that are known to use less RAM than their alternatives
By choosing the right combination of software and mindful usage habits, we can get the most out of our existing RAM. It’s important to make sure that the free command shows minimal or no swap usage.
5.2. Adjusting Swappiness
The swappiness parameter in Linux systems controls the kernel’s tendency to use swap space. By default, its value is 60:
$ cat /proc/sys/vm/swappiness
60
We can set swappiness from 0 to 100. A low value means that the kernel tries to avoid swapping as long as possible, while a higher value causes the kernel to aggressively try to use swap space.
For example, we can temporarily set the swappiness value to 40:
$ sudo sysctl vm.swappiness=40
vm.swappiness = 40
$ cat /proc/sys/vm/swappiness
40
We can test if this value is appropriate for our case by testing our applications and monitoring swap usage. We can also gradually try other lower values. However, setting swappiness incorrectly can degrade performance or have a different effect between light and heavy workloads.
To make this change permanent, we need to edit the /etc/sysctl.conf file by adding or modifying the line vm.swappiness=40. To apply the changes, we need to run sudo sysctl -p:
$ sudo nano /etc/sysctl.conf
vm.swappiness=40
$ sudo sysctl -p
If the swappiness value is too low, the system may run out of memory more quickly, possibly leading to out-of-memory (OOM) conditions where the kernel may kill processes to free memory.
5.3. zswap
zswap is a lightweight compressed cache for swap pages. On systems that use SSDs as swap devices, zswap can dramatically reduce the number of writes to the SSD.
zswap is disabled by default. To enable it at boot time, we need to add zswap.enabled=1 to the kernel boot parameters. In addition, we can enable zswap at runtime:
$ echo 1 | sudo tee /sys/module/zswap/parameters/enabled
1
$ dmesg | grep zswap
[ 1.211279] zswap: loaded using pool lzo/zbud
zram provides similar memory compression benefits to zswap, but they’re not the same. zram increases the amount of usable memory by compressing the data stored in RAM. On the other hand, zswap acts as a compressed cache for swap pages, holding them in a compressed form in RAM before eventually writing them to the swap space on disk.
It’s important to note that we shouldn’t use zram and zswap at the same time, as they handle memory compression in different ways and could interfere with each other, resulting in suboptimal performance or system instability.
6. Conclusion
In this article, we discussed how heavy use of a swap partition on an SSD can lead to increased write operations, performance degradation, and potential thermal stress, ultimately affecting the longevity of the SSD.
Key recommendations include ensuring adequate RAM allocation to reduce reliance on swap space, adjusting the swappiness parameter to control the system’s tendency to use swap, and considering the use of zswap for efficient memory management. These strategies can help optimize system performance and preserve SSD lifespan.