36

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.

7 Answers 7

31

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.

2
  • With a file system, one can TRIM to reduce write amplification. Is there an equivalent for a SWAP partition?
    – Bryce
    Apr 21, 2015 at 17:43
  • 2
    This answer seems to assume that "swappiness" ranges from 0 to 100 with zero meaning no swapping (and 100 meaning no RAM?). But, another answer goes into further detail about the swappiness value, indicating that the number ranges from 0 to 200 and represents a relative weight toward different types of swapping.
    – palswim
    Aug 24, 2017 at 6:10
8

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 file systems. 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 commercially, 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.

3
  • SSDs meant for servers can take more writes than the (cheaper) ones meant for consumers.
    – jarno
    Feb 4, 2016 at 10:04
  • 1
    Understood, but I have consumer SSDs installed in workstations that run 24x7 deployed across the enterprise and we backup the deltas every couple of days but haven't had a single failure in 2 years. Keep in mind that the MTBF for a physical spinning platter is 3 years and in my experience of selling/servicing systems for 25+ years, that's pretty accurate. SSDs outperform all other options, commercial or consumer.
    – TekOps
    Feb 5, 2016 at 17:20
  • Out of curiosity, what swappiness did you choose, if ypu don't actually care that the ssd is used to the max and hopefully help with performance? Aug 13, 2021 at 2:14
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

3
  • I would like to know what is wrong? The OP asked if he needs to change his swappiness to increase SSD life, and the answer is yes he does need to change swappiness.
    – Mitch
    Jan 12, 2014 at 17:46
  • I understand that the latest versions of Ubuntu automatically use TRIM for SSDs. Jan 14, 2014 at 19:06
  • The latest Ubuntu only uses TRIM for certain models of SSD's. @PaddyLandau
    – Bryce
    Apr 21, 2015 at 17:44
2

Is it true that setting swappiness to zero will increase the life of my SSD?

It would cause that Linux move memory blocks less frequently to the swap partition. Writing to the swap normally uses write cycles if your swap is in the SSD. Remember that the swap is only used when needed and if you have enough RAM you may be better without it (unless you want hibernation, but that's another issue).

The effects of this actions may vary, but doing it just to save some write cycles in the SSD is doing it wrong. Modern SSD's (about from 2012 onwards) have tons of write cycles, and the performance might be reduced for your applications when you are almost using up all your RAM. You use more write cycles during installation of programs than due swap. Remember, again, that swap is only used when is needed by default.

If you are worried about your SSD write cycles used for swap you may remove/deactivate the swap altogether if you have enough RAM and only activate a temporary swap in a HDD instead of messing with the swapiness. That would be the best route.

0

I would high recommend you to change swappiness setting to lower value, the best is "0" if you have enough memory/ram for your system 4 GB of ram or more. Even though swappiness at 10 with high ram system, i barely see ubuntu put anything on swap partition since mostly everything are on ram already.

I did a little bit google search and i found this page http://namhuy.net/1563/how-to-tweak-and-optimize-ssd-for-ubuntu-linux-mint.html

might help you to tweak and optimize SSD for your Ubuntu system run more efficiently and keep your ssd live longer

0

Changing swappiness may not actually have an impact. We have played with it a lot in systems that are short on RAM and we are wanting to push as much as possible to the SSD and it has actually made little impact, the items remaining in memory (which means they are needed). This may be a moot point.

0

I liked the idea of checking the smartctl output, however, my code is a bit more compact. I have never understood why people just do not know that awk has the grep capability built into it.

sudo smartctl -a /dev/sdb | awk ' ($0 ~ /Total_LBAs_Written/) {print ($10*512)/(1024^3) " GiB"}'

Though this is the code I use:

sudo smartctl -a ${device} | awk ' ($0 ~ /Total_LBAs_Written/) { if (($10*512)/(1024^3) > 1000) { msg="TiB"; size=($10*512)/(1024^4); } else { msg="GiB"; size=($10*512)/(1024^3); }; printf "\nThis device has had %.2
f %s written to it.\n",size,msg; }'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .