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?

15 Volts
  • 2,069
  • 1
    Related: https://unix.stackexchange.com/q/2658/237982 – jesse_b Jun 14 '19 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? – 15 Volts Jun 14 '19 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 '19 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. ¯_(ツ)_/¯ – SurpriseDog Jun 14 '19 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 '19 at 21:23
  • Thanks @Randall, I have read it, but say for example, you are copying a large file, no matter what available memory you have, and regardless of the swappiness (https://wiki.archlinux.org/index.php/Swap#Swappiness), it will always write something to the swap. For me, if the file is 5 - 6 GiB, around 40 - 50 MiB is written to the swap even if I have 3 / 4 gigs of available memory!

    Here's the problem: https://unix.stackexchange.com/questions/510997/why-does-linux-write-to-swap-space-even-if-the-system-has-available-storage-whil

    – 15 Volts Jun 15 '19 at 04:46

1 Answers1

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

somebody
  • 343
  • 3
    Easier said than done if OP has a laptop with only one drive slot. – jesse_b Jun 14 '19 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!

    – 15 Volts Jun 14 '19 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 '19 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 '19 at 20:22
  • @StephenKitt SSDs are fast so fast with swap ⚡️ but it's a very good suggestion not to get obsessed with it I would restrict myself. Or I can write a simple program to detect if there's any swap being used, and if the system has right amount of available RAM, turn off and on the swap to move the pages to the RAM! That way it will do the job automatically! So... let's see if it affects the running system! – 15 Volts Jun 14 '19 at 20:34
  • 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 '19 at 20:38