Hacker News new | ask | show | jobs
by stncls 746 days ago
The article is from 2018 and has had interesting discussion here before [1].

My conclusion is: In production, in a datacenter, when code is stable and compute-per-dollar efficiency matters? Yeah, sure, I can believe that swap makes sense.

On a dev machine? Not on mine for sure. If you think swap is a net positive on a dev machine, then try pasting the following (wrong) code in a bash prompt on Linux:

  echo '
  #include <stdlib.h>
  #include <stdio.h>
  #include <string.h>
  
  int main()
  {
    for (int i = 0; ; i++) {
      char *mem = malloc(1 << 30);
      if (mem == NULL)
        return 0;
      memset(mem, 42, 1 << 30);
      printf("%5d GiB\n", i);
    }
  }
  ' | cc -O3 -o crash_my_laptop -x c -
  ./crash_my_laptop
We can discuss the results in a couple of hours, when you recover control of your machine. (Note: with no swap and no z-ram, it crashes after 10 seconds; no side effects.)

[1]

https://news.ycombinator.com/item?id=40582029

https://news.ycombinator.com/item?id=39650114

https://news.ycombinator.com/item?id=38263901

https://news.ycombinator.com/item?id=31104126

https://news.ycombinator.com/item?id=29159755

https://news.ycombinator.com/item?id=23455051

https://news.ycombinator.com/item?id=16145294

https://news.ycombinator.com/item?id=16109058

3 comments

> On a dev machine? Not on mine for sure. If you think swap is a net positive on a dev machine, then try pasting the following (wrong) code in a bash prompt on Linux:

> We can discuss the results in a couple of hours, when you recover control of your machine. (Note: with no swap and no z-ram, it crashes after 10 seconds; no side effects.)

That might be true for this contrived example. But my real-world experience is exactly the opposite.

In a case of memory over-usage (in my case because of working in a huge bazel project and several browsers open + some electron apps):

- without swap, the system at one point just got immediately so incredibly slow, that the only thing I could reasonably still do was to restart my machine, while

- with swap, the system (at higher memory usage) just got noticably slower and I could close some app / the browser to get into the normal usable regime again.

My real world experience is the same as GP's and the opposite of yours.

In case of memory over-usage:

With swap: system becomes unresponsive

Without swap: OOM kills offending process

This has been my real world experience 100% of the time on Linux.

I'll add that swap is required for certain sleep modes (hibernate) so there are reasons to use it. I typically create a swap partition that's 1.2x system RAM on laptops for this reason, but don't for desktops (which also usually have more system RAM).
Let's be honest, users of this website are literally the worst possible group to reason about "typical PC usage"