Live data from Hacker News

Linux Performance: Why You Should Almost Always Add Swap Space

haydenjames.io

11–20 of 120 posts

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

#12

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.

The article specifically lists reasons why you should enable swap when there is plenty of RAM. Can you elaborate on what part of the article's reasoning you disagree with?

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

#13
post #6

Earlier quoted context omitted.

The most compelling reason to enable at least some swap space is that the Linux kernel's memory handling algorithms can't cope with there being no swap. If you have no swap, then you will encounter stalls and OOMs even if you don't actually run out of memory. Adding even a tiny amount of swap prevents this from happening.

i agree with this , lack of swap makes oom too aggressive . , question what's worst no app (and react to OOM) or app swapping

OOM is tunable.

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

#14
post #5
post #2

I'm sorry, but not a single reason was presented in favour of swap when you're 100% confident that you will not max out your RAM. Swap = completely useless slowdown if you have total control over your system. Better use ulimit to prevent processes with e.g. memory leaks from causing havoc.

Can you ever be 100% confident?

Yes. ulimit and cgroups do wonders.

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

#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?

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

#16

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.

Disk is still way cheaper than RAM, and I always add some swap space to my servers just to lessen the chance of getting ENOMEM errors if/when an unexpected or unusual workload would come along.

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

#17
post #6

Earlier quoted context omitted.

The most compelling reason to enable at least some swap space is that the Linux kernel's memory handling algorithms can't cope with there being no swap. If you have no swap, then you will encounter stalls and OOMs even if you don't actually run out of memory. Adding even a tiny amount of swap prevents this from happening.

This sounds as if it is a serious issue and should be fixed. Is there a bug report about it?

This is due to memory overcommit, it's a performance feature. You can turn it off.

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

#18
I always see people caring way to much about the amount of swap space used and not enough about the swap activity going on. It isn't the amount of swap space that slows a system down but actual swapping in/out of memory pages. In vmstat you want to pay attention to the "si" and "so" columns and in your alerting/graphing you want to keep track of the values "pswpin" and "pswpout" in /proc/vmstat. If these are almost always exactly or near zero then that means the fact that some memory pages are swapped out has virtually no impact on the performance of your system.

There are two other important issues to take into account though. 1) Even if the memory pages swapped out are not accessed generally they might be forces back into memory because of some specific action. One example is a database that has all the hot records in memory but other records that are accessed only very rarely swapped out to disk. In general everything will perform fine in this situation but the moment you do e.g. a table scan and a lot of these records need to be moved back into memory you might see a disk I/O peak that might be quite a kick on the neck for overall performance if the database is really busy.

2) If you don't have any swap space configured you might still run into problems with swap which seem to be caused by a bug in the memory handling in the kernel. I've seen this on some KVM hypervisors which were CentOS 7 systems. These systems were equipped with 128G of RAM and had two virtual machines running which both were configured with 32G of virtual RAM. They ran fine until one day the kswapd kernel process ran with 100% cpu usage even though no swap swap was configured whatsoever (to avoid the situation mentioned above). The "fix" was to dump the systems caches with "echo 3 > /proc/sys/vm/drop_caches" which seemed to calm down kswapd again. As best as I can tell what happened is that the system used all the free ram for the page cache and buffers and when the system needed some memory it apparently prefered to swap pages out to disk (even though no swap was configured) rather than reclaiming page cache of which there was plenty to reclaim. Unfortunately that means there seems to be no bulletproof way to say "only use physical ram and never try to swap anything out to disk". Even /proc/sys/vm/swappiness can be dangerous as a value of "0" doesn't actually tell the system to only swap if absolutely necessary but can lead to OOM situations even if swap space is still available (see https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux... for details).

TL;DR: 1) Don't just pay attention to the amount of swap space but to actual swap activity over time 2) Be aware of corner cases and bugs relating to swap

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

#19

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.

> It’s just there for people who can’t afford more RAM

So I should throw away my 5-year-old notebook that's still working perfectly fine just because the RAM is soldered in? Doesn't sound like efficient resource usage (both money and rare earths).

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

#20
When running an instance where the biggest process is a garbage collected runtime, one does not want any swap at all. If the garbage collector is forced to walk through all of the pages in swap hunting for live references, performance is terrible. It is better then to have the process see only as much virtual memory as there is RAM in the system. It is a failure of Unix/Linux that there is no sensible API for managing real RAM from the point of view of the process.
Post reply on HN