With an SSD, do I need to change my swappiness to increase SSD life?
I encountered this article which includes the following suggestion:
Swap and swappiness
I kept my swap partition on the old HDD. Swap on the SSD would be faster, but would also shorten the SSD’s life.
My system has plenty of memory, so I also reduced the swappiness value to 0 to tell Ubuntu to only swap when absolutely necessary. I did this by adding the line vm.swappiness=0 to /etc/sysctl.conf.
Is it true that setting swappiness to zero will increase the life of my SSD? I can't find references to this elsewhere.
Solution 1:
Reducing the swappiness to 0 means that the OS will only start using the SSD for swap when there is no more available free RAM.
Theoretically this can increase the SSD's lifetime because the SSD will suffer less writes. It depends whether it is practically increases the SSD's lifetime:
Linux rarely use the swap (especially compared to windows) even with higher swappiness values too, so this setting likely won't change the swapping behaviour on systems with plenty of RAM compared to RAM usage. If you are short on RAM then swapping can happen quite frequently with the default value of 60, in this case decreasing swappiness will likely have a significant impact on the SSD's write cycle limit.
The SSD's write endurance is still pretty high for a simple consumer usage, at least for MLC based SSDs. They say that the current MLC based SSD's can tolerate ca. 3000-5000 write cycles before they wore out. Calculating with the 3000 limit that means that a 128 GB SSD with a daily 10 GB writes will work happily for ca. 38000 days, that means ca. 100 years. If you have a bigger SSD of course that will wore out much later. So it is very unlikely that your SSD will die because of the cells wearing out. (Of course if you use your SSD in a corporate/server environment then the daily writes are much higher, but in corporate environments usually SLC based SSD's are used.)
- This write cycle limit is so high that the SSD manufacturers (at first SAMSUNG) started to manufacture TLC based SSD's for consumers, which can only tolerate ca. 1000 write cycles, but are cheaper. These companies say that even these SSD's write cycle limit will last longer than the time the product is expected to be used. (It is unlikely that in 2024 we will be still using our 128 GB SSD's) Although if you have a TLC based SSD and you write quite frequently to that drive, e.g 50GB/day, than decreasing the swappiness value will likely have a significant effect on your SSD's life length.
- Here is a giant thread where enthusiasts tested the SSD's endurance by constantly writing to them. Although their test method is probably not totally correct, their results are quite impressive and soothing.
So to sum up: In theory yes, decreasing the swappiness will result in longer SSD lifespan, in practice it will likely not. But if you have a TLC based SSD and you are short on RAM and you generally write frequently to the SSD then it can have a practical effect too.
I have an MLC based, 128 GB SSD and I mostly use my system for office/browsing and I have plenty of RAM compared to my RAM usage so my SSD is not under heavy write load (although my machine is on 24/7). But because I don't need swapping at all and it is so easy to lower the swappiness I did for myself set swappiness to 0 when I installed my SSDs, because why not do it? (Actually I set swappiness to 0 on all of my machines, even on machines which only have HDDs.)
You can check how much write has your SSD suffered during it's life with smartctl
, look for the line with Total_LBAs_Written
, this will show you how much 512b sectors have been written to the SSD. (Yes I know that the SSDs have physically 4K blocks, not 512b, but SMART still says: Sector Size: 512 bytes logical/physical
and with 512 blocks I get results which are comparable to the tune2fs -l
Lifetime writes output.) So multiple that number with 512 and you get the writes in byte, or for short:
sudo smartctl -a /dev/sda | grep Total_LBAs_Written | awk '{print $10*512/1024/1024/1024 " GiB"}'
will print out in GiBs for /dev/sda. (Just change the device name to your SSD's name.) My 128 GB SSD has suffered ca. 800 GB writes in a little more than one year, that means only 7 write cycles used from the 3000.
Solution 2:
All of this talk about wearing out SSDs is "guesswork". I had the same feelings, but started using them commercially. I have one of the fastest oracle clusters on the east coast. 3 years ago we instituted SSDs for our entire storage AND temporary filesystems. We do billions of write operations/day to the SSDs. After 3 years we still have not come anywhere near 10% burnout. If I can do that commerically, they will NEVER wear out in a residential situation. So-- I implemented an SSD solution specifically for swap space on my linux workstation and BAM!!!
So, your mileage my vary but SSD is the way to go in every way.
Dave
Solution 3:
Yes. You do need to change swappiness to increase SSD life.
Swappiness lets you control how much swap file is being used. Swappiness values can be changed from 0 to 100. The higher swappiness values the more the kernel will try to use swap space, the lower swappiness values means the kernel will use less or no swap space depending on our setting.
The default swappiness value from is 60, if you have plenty have RAM, you should avoid using swap space which writes and reads to your SSD, but to an HDD. For system with 4 GB or more RAM, try to reduce the usage of swap by changing swappiness settings to between 10 even 0, with the swap file being on an HDD.
Running a swap space is very disk I/O intensive, and that’s actually bad for an SSD. An SSD can only handle so many writes before it goes bad and you’re forced to buy another drive. So, if at all possible, move your swap space to a secondary, spinning drive…away from the SSD.
A Solid State Drive is worn down relatively quickly by write actions. Especially the oldest generations of SSD's were vulnerable in that aspect, but to a lesser degree that's still the case for the newer generations.
You can either make a new partition on a new disk, or just make a swap file instead; making a new partition will almost always result in much better performance.
If you don’t have an alternate drive to move to, you can alter a setting that will only enable swap when your physical RAM is 100% full, which will decrease the load on your swapfile, and help your SSD. Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command(s) below:
echo 0 > /proc/sys/vm/swappiness
SSD wear and tear has to do with write cycles
A Solid State Drive is worn down relatively quickly by write actions. Especially the oldest generations of SSD's were vulnerable in that aspect, but to a lesser degree that's still the case for the newer generations.
Use Trim
TRIM is an advantage that you use with SSDs. It runs in software and communicates with the SSD controller, telling it which ‘blocks’ are no longer needed by the filesystem and can be safely cleaned and overwritten. This won’t really result in an improvement of performance, but will rather lengthen the life of your SSD.
Sources:Linux SSD Tweaks & How-to for optimizing your SSD