Should swappiness be lowered for a SWAP partition on an SSD?

As the results I found on this are inconclusive, I wanted to bring up the question here as well just to hear an extra opinion. I use an SSD for my root drive, with the SWAP partition being 4 GB large and the system having a total of 16 GB of RAM. Technically I don’t even need SWAP with so much RAM, but just in case my RAM ever fills up I wanted to have a little safety margin. Although my SSD is a recent model and high quality MLC drive, I’m still on the lookout to avoid writing to it more than I need to, as to not wear out its relatively sensible lifespan. Although some openSUSE Tumbleweed snapshots can dump +1 GB of updates in one day (eg: game packages) my attention has been on SWAP as the default configuration causes it to be used very quickly.

The default swappiness is 60. What bothered me about it is that, whenever the system gets past roughly 6 GB of used memory, it begins filling the SWAP at once. With some software it can reach and stay at over 9 GB of RAM for a while, which in a few hours causes the SWAP to be filled up to 100 MB. This is pretty fast for barely exceeding half of my available RAM! As this makes rather nervous I decided to edit /etc/sysctl.conf and readd the commonly recommended vm.swappiness = 10 at the end.

Like I said I wanted to hear second thoughts just to know if this is the right approach: Do you believe the default swappiness of 60 is considered acceptable for SWAP partitions on SSD drives? Or do others agree that for an SSD a swappiness of 10 is better?

Rule: Do not change the defaults. They are there by design and for the safety of you and your systems.

Hi
I change mine to use ram before swap with a /etc/sysctl.d/98-<hostname>.conf


#disable swap
vm.swappiness=1
vm.vfs_cache_pressure=50

I’m only running < 4GB swap on ssd’s with 8 and 16GB of ram…

It all depends on what your doing, swap can save a headache with a runaway process if I’m working on something, so can either run systemd-zram-service (A package I built/maintain), there are a few other zram tools/services available. Or just create a swap file to use on the fly and delete when finished…

Fun fact: It seems that even with a swappiness of 10, I still get roughly 10 MB of SWAP being used once my system RAM exceeds 8 GB from my total of 16 GB. This is a lot less though and something I find acceptable, I’ll be leaving it at that but it’s interesting to know this.

I agree with that as a general rule. In this case I know what I’m doing, and swappiness is a safe setting to adjust especially if you have enough RAM. I went without having any SWAP at all for a few years… main reason why I added it back is that I switched from a motherboard with 24 GB of RAM to one with only 16 GB, due to the old one being DDR3 and the new one using DDR4 memory which I couldn’t afford to buy as much of.

First,
I don’t know of any risk or danger in modifying swappiness, AFAIK it’s a setting that modifies only the tendency of swap functionality and even at its extreme settings impose no hard limits.

As for whether you should change the setting, I tend to say not to do so. You can tinker if you wish, but unless your setup is unusual or you’re doing something very specific that’s not “typical” what you do probably won’t affect the system much.

Back a ways, but not very far back I came to understand that how the OS uses the swap file had changed and wouldn’t surprise me if has changed in more ways since. You may think that swap is used mainly for scenarios when you have inadequate physical RAM, but today that’s only a small part of the equation, at least as important is that modern OS has learned to use RAM in more efficient ways. One important way might be to write any seldom used data to disk, leaving more addresses available for actively running processes. This would be especially true immediately after boot, when a number of processes that might have run during boot might have resulted in processed data that might be used only rarely but should not be deleted. So, write to swap.

This might also be true in modern Linux where as much as possible seems to be mounted in RAM using tmpfs which will fill up your physical RAM quickly. That’s good if your applications can use the data in those mount points but not so good if the data is rarely or not used at all. So, maybe some of this might be written to swap? I’d hope so.

But, those and similar OS optimization are what other people who create and maintain Linux do, I’m perfectly happy to allow them to make these kinds of decisions for me until I experience some kind of noticeable latency.

TSU

I am using 2 SSD and one rotating disk and 16 Gig Ram.
My swap is 20 Gig in the rotating disk.:wink:

Hi
Have a read here: https://forums.opensuse.org/showthread.php/538177-HP-8570plaptop-16g-of-ram-and-swap

I have 32 GB of RAM and use a 1 TB SSD for my OS install. With that much RAM, I haven’t used a swap partition in quite a few years. I’ve never had a single problem. That’s 32 GB of RAM vs your 16 GB so I don’t know how that compares as far as the need for swap, but I figured I’d mention my experience anyway.

I’m planning to upgrade to 32 GB myself once I can afford another set of two RAM boards identical to my current two. In the meantime 16 GB is just enough. Only problem is that since I easily reach half of that, it will trigger the SWAP… which isn’t even affecting my performance, but I’m concerned about my drive being written to more often than I feel it needs to be.

Hi
What are the specs for your SSD… I wouldn’t even worry about it, there are probably more writes to the journal…

Here is the specs section of the store and page I bought it from: https://www.emag.ro/solid-state-drive-ssd-adata-ultimate-su900-512gb-2-5-sata-iii-asu900ss-512gm-c/pd/D3YYS7BBM/?ref=fam#specification-section

Hi
So how many hours does the device have on it, how many bytes have been written?

Your specs show 800 TB of data written is the expected lifetime… based on that and my current nvme device from smartctl, I see;


Data Units Written:                 2,353,652 [1.20 TB]
Power On Hours:                     2,085

Based on your specs the above drive say 1 TB per 2000 hours times 800 = 1,600,000 hours, which equates to around 182 years… if you turn it around and say I want the device to last say 5 years, that’s 160 TB per year or about 438 GB a day or 18 GB an hour…

My NVMe device is writing 0.575GB per hour or 13.8GB a day… most of (OCZ, SanDisk, WD and Crucial devices) mine are the same eg 0.5GB an hour I just don’t worry about it…

My oldest SSD (OCZ 60GB) still running fine it has one (1) retired block and SSD life curve is still at 100.


  9 Power_On_Hours_and_Msec 0x0032   037   037   000    Old_age   Always       -       55585h+18m+37.730s
241 Lifetime_Writes_GiB     0x0032   000   000   000    Old_age   Always       -       25947

mircea@linux-qz0r:~> sudo smartctl -a /dev/sda                  
[sudo] password for root: 
smartctl 7.0 2019-05-21 r4917 [x86_64-linux-5.3.12-1-default] (SUSE RPM)
Copyright (C) 2002-18, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Device Model:     ADATA SU900
Serial Number:    2I4220083049
LU WWN Device Id: 5 707c18 1006f921e
Firmware Version: Q0125A
User Capacity:    256,060,514,304 bytes [256 GB]
Sector Size:      512 bytes logical/physical
Rotation Rate:    Solid State Device
Device is:        Not in smartctl database [for details use: -P showall]
ATA Version is:   ACS-2 (minor revision not indicated)
SATA Version is:  SATA 3.1, 6.0 Gb/s (current: 6.0 Gb/s)
Local Time is:    Sun Dec  8 02:12:36 2019 EET
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED

General SMART Values:
Offline data collection status:  (0x02) Offline data collection activity
                                        was completed without error.
                                        Auto Offline Data Collection: Disabled.
Self-test execution status:      (   0) The previous self-test routine completed
                                        without error or no self-test has ever 
                                        been run.
Total time to complete Offline 
data collection:                (  171) seconds.
Offline data collection
capabilities:                    (0x71) SMART execute Offline immediate.
                                        No Auto Offline data collection support.
                                        Suspend Offline collection upon new
                                        command.
                                        No Offline surface scan supported.
                                        Self-test supported.
                                        Conveyance Self-test supported.
                                        Selective Self-test supported.
SMART capabilities:            (0x0002) Does not save SMART data before
                                        entering power-saving mode.
                                        Supports SMART auto save timer.
Error logging capability:        (0x01) Error logging supported.
                                        General Purpose Logging supported.
Short self-test routine 
recommended polling time:        (   2) minutes.
Extended self-test routine
recommended polling time:        (  10) minutes.
Conveyance self-test routine
recommended polling time:        (   2) minutes.
SCT capabilities:              (0x003d) SCT Status supported.
                                        SCT Error Recovery Control supported.
                                        SCT Feature Control supported.
                                        SCT Data Table supported.

SMART Attributes Data Structure revision number: 1
Vendor Specific SMART Attributes with Thresholds:
ID# ATTRIBUTE_NAME          FLAG     VALUE WORST THRESH TYPE      UPDATED  WHEN_FAILED RAW_VALUE
  1 Raw_Read_Error_Rate     0x0000   100   100   000    Old_age   Offline      -       0
  5 Reallocated_Sector_Ct   0x0000   100   100   000    Old_age   Offline      -       14
  9 Power_On_Hours          0x0000   100   100   000    Old_age   Offline      -       9386
 12 Power_Cycle_Count       0x0000   100   100   000    Old_age   Offline      -       98
160 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       0
161 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       22
163 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       13
164 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       18377
165 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       73
166 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       5
167 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       35
148 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       778
149 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       65
150 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       0
151 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       51
169 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       100
177 Wear_Leveling_Count     0x0000   100   100   050    Old_age   Offline      -       0
181 Program_Fail_Cnt_Total  0x0000   100   100   000    Old_age   Offline      -       20
182 Erase_Fail_Count_Total  0x0000   100   100   000    Old_age   Offline      -       0
187 Reported_Uncorrect      0x0000   100   100   000    Old_age   Offline      -       0
192 Power-Off_Retract_Count 0x0000   100   100   000    Old_age   Offline      -       38
194 Temperature_Celsius     0x0000   100   100   000    Old_age   Offline      -       51
196 Reallocated_Event_Count 0x0000   100   100   016    Old_age   Offline      -       0
199 UDMA_CRC_Error_Count    0x0000   100   100   050    Old_age   Offline      -       0
232 Available_Reservd_Space 0x0000   100   100   000    Old_age   Offline      -       91
241 Total_LBAs_Written      0x0000   100   100   000    Old_age   Offline      -       100633
242 Total_LBAs_Read         0x0000   100   100   000    Old_age   Offline      -       27564
245 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       294032
248 Unknown_Attribute       0x0000   100   100   000    Old_age   Offline      -       920758

SMART Error Log Version: 1
Invalid Error Log index = 0x07 (T13/1321D rev 1c Section 8.41.6.8.2.2 gives valid range from 1 to 5)

SMART Self-test log structure revision number 1
Num  Test_Description    Status                  Remaining  LifeTime(hours)  LBA_of_first_error
# 1  Short offline       Completed without error       00%       170         -
# 2  Short offline       Completed without error       00%       147         -
# 3  Short offline       Completed without error       00%       123         -
# 4  Short offline       Completed without error       00%        99         -
# 5  Short offline       Completed without error       00%        75         -
# 6  Short offline       Completed without error       00%        51         -
# 7  Short offline       Completed without error       00%        27         -
# 8  Extended offline    Completed without error       00%         3         -
# 9  Short offline       Completed without error       00%         1         -
#10  Short offline       Completed without error       00%       235         -
#11  Short offline       Completed without error       00%       211         -
#12  Short offline       Completed without error       00%       187         -
#13  Short offline       Completed without error       00%       163         -
#14  Short offline       Completed without error       00%       139         -
#15  Short offline       Completed without error       00%       115         -
#16  Short offline       Completed without error       00%        90         -
#17  Short offline       Completed without error       00%        66         -
#18  Short offline       Completed without error       00%        42         -
#19  Short offline       Completed without error       00%        18         -
#20  Short offline       Completed without error       00%       251         -
#21  Short offline       Completed without error       00%       227         -

SMART Selective self-test log data structure revision number 1
 SPAN  MIN_LBA  MAX_LBA  CURRENT_TEST_STATUS
    1        0        0  Not_testing
    2        0        0  Not_testing
    3        0        0  Not_testing
    4        0        0  Not_testing
    5        0        0  Not_testing
    7        0    65535  Read_scanning was completed without error
Selective self-test flags (0x0):
  After scanning selected spans, do NOT read-scan remainder of disk.
If Selective self-test is pending on power-up, resume after 0 minute delay.

Hi
You need to find out from Adata what their calculation for “Total_LBAs_Written” is to calculate out how much has been written, but I expect not that much per hour, as well as nothing to worry about…

Interesting thread. I just came from Debian and other Debian based systems and now I’m thinking to reinstall my openSUSE with swap, lol. So basically I see this issue from the other side, I also prefer giving decent swap base for Linux systems however around 3 years ago I’ve had serious freezing issues on Debian but a lot has changed since then. I think my case was related to SSD’s firmware and kernel
https://wiki.debian.org/SSDOptimizationhttps://askubuntu.com/questions/41778/computer-freezing-on-almost-full-ram-possibly-disk-cache-problem](https://wiki.debian.org/SSDOptimization)At the end, I solved it with a drastic vm.swappiness=1 in /etc/sysctl.d/99-sysctl.conf, I had to. Since then, I installed every distro without RAM, even my current and very first Tumbleweed few days ago. Now I’m just thinking if I should try to install it with swap, despite I’ve 32GB RAM

Ok, I just decided not to reinstall my system without swap just to get a swap partition. Although you’re able to almost turn swap off, my current installation just seems quick and stable enough for now. Also I’ll decrease the amount of unnecessary snapshots.
It’s an old post but ti’s still worth to mention
https://lizards.opensuse.org/2015/02/06/ssd-configuration-for-opensuse/

Article very outdated & unreliable concerning I/O scheduler, ssd wear & tear, snapshots (!!!).

Up to date (as of 2020-06-10) information:

  • It’s fine to have some swap space on SSD
    • Match to workload memory size if you want to use suspend-to-disk
  • It’s possible to configure vm.swappiness to reduce swapping
  • Let snapper handle system snapshots (snapshots do not write more data, they do result in more space being used from data already written).
  • openSUSE by default will pick an appropriate scheduler

A bit unrelated but in my machines with high RAM (>16GB), I used to put swap partition on a spinning HDD and my swapiness down to 1 or 0. Also, the result wasn’t pretty, as soon as my system needed to use the SWAP, it would freeze and/or crash. I never had this issue when I had set swappiness to 10 in 8GB RAM machine.