1

I have a system with 4 gigs of RAM and an SSD. I heard allocating swap on SSD is a bad idea because SSD has got limited write cycle.

So when I see < 100 MiB of pages is written to the swap, and I have some available space on the RAM, I just use an alias 'rswap' to turn it off and on again:

alias rswap='sudo swapoff /dev/sda3 && sudo swapon /dev/sda3'

Now this moves the pages back to RAM, and the swap gets down to 0 bytes. The RAM usage increases, but I never had a problem with applications so far.

Also, there are several reasons why the swap is used despite having available RAM. One of them is this one!

Is it a good idea to run the aliased command rswap repeatedly?

  • 1
    Related: unix.stackexchange.com/q/2658/237982 – Jesse_b Jun 14 at 18:58
  • Thank you @Jesse_b, but my question is does turning off and on the swap affect the SSD and overall, the system? – S.Goswami Jun 14 at 19:02
  • 1
    That is why I didn't link it as a possible duplicate, but that question will help you change the settings to make your system use less swap. – Jesse_b Jun 14 at 19:03
  • 1
    That thread is almost 10 years old now and IMHO the prevalence of cheap memory and fast SSDs have made a lot of the need for swap redundant. In my experience what happens when I enable swap is that it will work fine for awhile and then an out of control process will eat up all of the memory and then I will have to wait while the the entire swap fills up bringing the system to a complete stop before the OOM kils it. I've been running with an SSD and no swap for years now and doing fine. ¯_(ツ)_/¯ – Benjamin Jun 14 at 20:25
  • 1
    @somebody and @Stephen are correct that unswapping that way is not what you want. But, take a look at this answer to a related question for a systematic way to direct the OS to reduce swapping by setting swappiness. – Randall Jun 14 at 21:23
3

In a word; no.

swap is there for a reason, and a good one at that. Besides, you're not really accomplishing your goal, by (repeatedly) unswapping. You're effectively doing what you're hoping to avoid.

Honestly. The real solution is to simply pick up a (pata) drive, or grab one you've got laying around, and use it solely for swap. Just about any drive would do, and given your size requirements. You should have no more than a 5 to 10 dollar investment, if you actually had to buy one. :)

HTH

  • 3
    Easier said than done if OP has a laptop with only one drive slot. – Jesse_b Jun 14 at 19:14
  • I am using a laptop :( I mean moving around the drive with the laptop, connecting it all the time can be a pain :( Though it's a good idea to connect another drive if you have a PC! – S.Goswami Jun 14 at 19:17
  • Sigh. That does present a problem. You might want to consider creating a swap file, as opposed to a swap node/slice/partition. That will at least minimize the (potential) damage. – somebody Jun 14 at 19:27
  • 1
    In practice you’ll never wear out an SSD, even if it’s used for swap, so there’s no need to deactivate swap or use a spinning drive just to preserve the SSD. – Stephen Kitt Jun 14 at 20:22
  • 3
    The thing is, once pages have been swapped out, you’ve already paid the price for that; swapping them back in is only going to make you pay it again in the future. But really you don’t need to worry about it at all. My oldest SSD, which hosts swap and some very active file systems, is 6.5 years old, has had 22 TiB written to it, and is only at 28% of its rated life according to its counters. – Stephen Kitt Jun 14 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.