Live data from Hacker News

Linux Performance: Why You Should Almost Always Add Swap Space

haydenjames.io

31–40 of 120 posts

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#31

If you must use swap (you probably don't need to), then at least set zswap.enabled=1 in the kernel boot options, typically in grub. This will enable lzo compression of swap in memory so there is less writing to disk. Some newer kernels use lz4. Either way, what most folks are actually missing is the correct kernel settings for the amount of memory they have. Sadly, the kernel does not dynamically adjust these based o…

Good stuff here.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#32
post #15

I wondered this for a while, unfortunately a major question I have remains unanswered in the text: What about SSDs and SSD-only systems? (Like... any average laptop.) Should I be worried that putting a swap on an SSD will cause it to wear out fast due to many write cycles?

I set vm.swappiness to 0 on my Fedora laptop.

Meaning the kernel will only swap to save the system.

I've been doing this for years and had no issues.

Edit: Actually in recent kernels, which I am using, 0 means it's disabled. I was thinking of 1.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#33
> As a last resort, the Kernel will deploy OOM killer to nuke high-memory process(es)

Yes! That is exactly what I want to happen!

When the system runs out of RAM, things will generally stop functioning, swap enabled or not. The only question is how you want it to stop functioning when that happens.

In almost every situation, I'll easily take the kernel killing whatever single process it thinks is most appropriate to get rid of, and keep everything else up and running smoothly, over grinding the the entire system to a halt by upping the effective memory access time by orders of magnitude.

Simply put: If everything doesn't fit in memory, then don't try to run everything!

Properly designed software nowadays is designed to be able to crash without corrupting data. As far as I'm concerned, it is almost always preferable to kill and restart instead of giving CPR to processes that don't fit in the working memory.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#34

Swap isn’t needed at all in this age. It’s just there for people who can’t afford more RAM and therefore have to resort to hacky solutions like this.

What an absurd statement.

Do you use a laptop? I do and I'm using swap space right now. I would like to buy a laptop with 32 or 64 Gigs of RAM but I can't as most(all?) laptop makers don't ship with a memory controller that permits more than 16 Gigs of RAM. And I can afford to buy more RAM.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#35
post #15

I wondered this for a while, unfortunately a major question I have remains unanswered in the text: What about SSDs and SSD-only systems? (Like... any average laptop.) Should I be worried that putting a swap on an SSD will cause it to wear out fast due to many write cycles?

I used to work in flash manufacturing. We would joke, "I hope nobody ever uses this for swap!" The numbers I saw for endurance (basically write-erase cycles before failure) were not encouraging at all. As far as I can tell the only reason MLC flash works at all is that the controller does some magic to present a whole bunch of pretty flaky cells as a single reliable unit.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#37
Swap engineering is definitely way more complicated than the article lets on.

What people often don't realise is that Linux distinguishes between dirty and clean memory, with dirty memory not reflected in swap, and clean memory already stored somewhere on disc, and program code is counted as clean memory that just happens to live somewhere other than swap.

Therefore, under memory pressure (especially if you set swappiness to zero), you will be preferentially swapping out your program code (because it is always clean) in preference to your program data. If you have no swap, then this is what causes the system to grind to a halt when the RAM is full- all your program code gets discarded from RAM, and nothing can run without reading it from disc again.

The recommendation to have a little bit of swap is absolutely fine. However, as the amount of RAM in your system increases, the penalty for running out of RAM increases as well. On larger systems (for example 256GB RAM), I recommend using something like EarlyOOM[1] to kill off tasks before the pathological swapping case occurs. Otherwise, you could end up with an unresponsive system. If you have lots of RAM, the kernel OOM killer waits far too late, and the system is already unresponsive.

[1] https://github.com/rfjakob/earlyoom

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#38
post #15

I wondered this for a while, unfortunately a major question I have remains unanswered in the text: What about SSDs and SSD-only systems? (Like... any average laptop.) Should I be worried that putting a swap on an SSD will cause it to wear out fast due to many write cycles?

I've swapped hundreds of terabytes to consumer SSDs. Haven't noticed any problems yet (visibly).

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#39
Nowadays linux has this thing called zram disk. It compresses data in memory on the fly. I think general swapping advice would serve most people better if they always add swap on zram first, before swap on ssd or hdd. The difference is that getting data in and out of zram is so fast that you can actually use it as a tier of a slower ram, transparently compressed.

Re: Linux Performance: Why You Should Almost Always Add Swap Space

#40
Interesting; Jeff Atwood of Coding Horror/Stackoverflow/Discourse forces swap creation as part of the installation of Discourse as a Docker container. He prefers slow performance to OOM. https://meta.discourse.org/t/create-a-swapfile-for-your-linu...
Post reply on HN