← Back to context

Comment by correct_horse

3 days ago

Not on nixOS, but I thought I would weigh in on my preferred value for swappiness. 0. For some reason when I have a month of uptime (and I suspend once a day), doing anything with the desktop environment for the first time in a few hours causes a micro stutter for maybe 100ms, as if having to swap out from disk. Turning swap off and on again (meaning no pages are swapped to disk anymore) immediately fixes it. Opening a new tab, pressing the super key even performing certain actions in a video game cause the stutter. I know the internet tells me that 0 isn't a good value, but my computer becomes noticeably worse to use even with a swappiness of 5. I have enough RAM for anything I want to use this computer for, so swap is just for suspend for me.

Agree. I tried using a swapfile last year-ish, as the internet experts tell you to. I have 64GB of RAM. If I fresh boot and start a few apps I'll be using maybe 16GB RAM + 0GB swap. Then a few hours later I see I'm using 8GB of RAM + 8GB of swap... why? Why did Linux waste IO moving data to disk when there's no memory pressure?

That's why I don't use swap.

  • Generally the idea is that regions of memory that aren't being used are swapped out to disk, then other more useful things are brought into memory.

    For example: you have a background program that you're not using and in the foreground you're crunching data from disk. In this scenario the most efficient use of your memory is to swap the background program out and pull the disk data into RAM.

    • It's a broken idea, throughput über alles isn't true of an interactive system, latency matters. There's a calculator app I use every now and then. Linux always thinks it's a good idea to swap it out since I'm not using it all the time, which means that every time I want to calculate something there's lag as it's swapped in again.

      2 replies →

    • But why should it swap things out even when I'm not crunching data and there's no memory pressure? I'd rather everything stay in memory and stay fast, since I have enough RAM.

      2 replies →

  • I suspect the intent is to move stale things out of RAM so it can be immediately used for more productive purposes when needed.

    Shuffling out 8GB with a spinning hard drive could take a full minute…

    That said, I agree with you. I’d rather not use swap unless absolutely necessary and I don’t want the latter to happen either…

    Plus, I don’t want my storage in the “critical path” of memory accesses, even if rare.

    • Use swap for the important things: the part of your operating system at run time which doesn't get used much, but needs to be around when needed - the contents of /lib and /usr/lib and so on, all the *.so's and the things they allocate - shared libraries among the distro install, these things can be tidily swapped, until you need it.

      If you need an application to always be responsive, start it with the swappable flag turned off, i.e. something like this:

          $ systemd-run --user --scope \
          -p MemorySwapMax=0 \
          -- /path/to/my/program arg1 arg2
      

      Anyway, this is a distro-originated knob which every user must tweak for their use-case.

    • > I suspect the intent is to move stale things out of RAM so it can be immediately used for more productive purposes when needed.

      These are separate problems, though. You can clean a page by updating its backing store without purging it from memory; and purging a clean page doesn’t require a write. It’s definitely reasonable to take advantage of idle IO resources to clean dirty pages, but pages should only be purged (clean pages first, usually) when there’s an immediate need for them for something else.