5

I'm running openSUSE TW, 32 GiB RAM, 64 GiB swap (SSD).

Swap usage is zero up until the point when RAM gets fully filled up with cache.

I'm using htop to monitor it. Yellow/orange = cache

My system would be using 2 GiB of RAM, but the rest (30 GiB) is all cache and then, instead of throwing away some cache to make room for other data when needed - it starts using swap, only a few MiBs, but I sware I can notice small freezes and the system sometimes slows down noticeably.

Why is that and what can I do about it?

I'm open to provide any requested info.


vmstat 60 5 vmstat 60 5

vm.swappiness = 1
vm.dirty_ratio = 20
vm.dirty_background_ratio = 10
TypicalHog
  • 51
  • 5
  • What about vm.dirty_ratio and vm.dirty_background_ratio settings ? – MC68020 Aug 13 '22 at 13:30
  • Unless it's absolutely necessary it's best to run without SWAP, period. Not a single person who advocates for the presence of SWAP has ever shown any test results from having it. SWAP on an SSD disk causes extra tear and wear. – Artem S. Tashkinov Aug 13 '22 at 14:45
  • @ArtemS.Tashkinov I did read somewhere that some programs might require swap to exist (no matter the size) to run. Don't know if true. I also had some issues when decompressing a large file, where my system would crash without swap. – TypicalHog Aug 13 '22 at 15:46
  • 1
    you're not doing much on the system (avg 96% idle, 100% idle during the measurement). We also see swapd is zero, so no swap use at that time. A measurement at times when you're doing things on the system would be more helpful. – Ljm Dullaart Aug 13 '22 at 15:51
  • "I sware I can notice small freezes and the system sometimes slows down noticeably." Apart from swaring… can you provide data ? And at first what are you running when noticing the freezes ? – MC68020 Aug 13 '22 at 15:54
  • @LjmDullaart Oh, I see. For some reason I just assumed vmstat would fill my RAM or something and stress test it. I didn't know I had to stress test it and vmstat just measures stuff. My bad, lol. – TypicalHog Aug 13 '22 at 15:59
  • @MC68020 Will report back soon. If I remember correctly, I was copying a lot of files and that seemed to cause cache to grow and start touching swap. – TypicalHog Aug 13 '22 at 16:01
  • Copying lot of files ? Then you might just have filled your cache with dirty data. Gone > dirty ratio forcing your app to issue blocking writes. Then actually reducing the throughput. additionally, since cached data would be important… don't expect your system to "throw it away" ! – MC68020 Aug 13 '22 at 16:07
  • @LjmDullaart I have updated the vmstat output under heavy load. (RAM was at like 20/32 GiB, while cache filled the rest and started using some swap) – TypicalHog Aug 13 '22 at 16:26
  • @MC68020 Would you say my dirty_ratio variables are fine or should I modify them? – TypicalHog Aug 13 '22 at 16:29
  • I'll write an answer – MC68020 Aug 13 '22 at 16:40
  • 1
    The case for swap is what you want to happen when the server's RAM is filled up with code/data segments from processes and there's a minimum allowable disk i/o cache reserved....and then a program requests more memory from the OS. Without swap the request will be denied, and most FOSS programs (and quite a few commercial ones) will crash in flames. With swap, the OS will start allocating from the disk device and the system will get slower. App slows down or hard crash? Cake Or Death? – Sotto Voce Aug 13 '22 at 18:07
  • 1
    64 GiB of swap is usually way more than you need on a system with plenty of RAM (16 GiB or more), unless you're running programs that need even more RAM than you have. (Usually that would be a performance disaster, unless a good fraction of that footprint isn't touched regularly). 4 to 8 GiB should be plenty to let the OS swap out "cold" dirty pages from typical desktop usage, leaving your actual RAM free for useful stuff. (I run 2GiB of swap on my desktop with 32G RAM; it's usually full, probably with pages from KDE crap and/or Chromium). – Peter Cordes Aug 13 '22 at 22:50
  • 1
    @SottoVoce: Linux overcommits memory by default (https://www.kernel.org/doc/Documentation/vm/overcommit-accounting), so allocation will only fail for really huge amounts of memory (or maybe if attempted with mmap(MAP_POPULATE) which prefaults the pages). So normally allocation succeeds, and more physical memory is needed only when a program writes to that allocated memory. If there isn't enough swap + RAM, the kernel has to kill something (OOM killer). – Peter Cordes Aug 13 '22 at 22:55
  • @PeterCordes a sudden death by the kernel OOM killer or by segfault trying to use non-existent memory after a malloc returned error (but wasn't checked) amounts to the same thing. What's the desired result - the daemon serving production traffic gets slower (from paging to/from slower SSD/HD), or the daemon suddenly dies? – Sotto Voce Aug 14 '22 at 00:03
  • @SottoVoce: Right, that's the tradeoff, but IMO it's a good thing to explain the correct mechanism so people can understand how their systems work. And it's not like "the OS will start allocating from the disk device"; instead it will try to evict (swap out) some existing allocations that haven't been touched recently. That's fine up until the point when too many pages are being used frequently and can't just sit there on disk. Then you get swap thrashing if you have swap space (or maybe to some degree even just compressed zswap to RAM), for a while before hard OOM if something keeps allocing – Peter Cordes Aug 14 '22 at 01:09
  • 1
    the "swap = 2 x RAM" recommendation was obsolete for more than a decade. Yes you do need at least some swap for many apps to work, but that means just a tiny amount like 512MB or 1GB is enough, or even better switch to zram or zswap like what many modern distros do. See How do I use swap space for emergencies only? – phuclv Aug 14 '22 at 07:53
  • @PeterCordes thanks - you have good points, but I elected to be concise because I was writing a comment intended for other folks in the comments, not an answer to the main question. As you know, comments are limited in size, so I didn't fully explain the precise mechanisms of an application's abrupt end after being denied more memory. The scope and context called for a summary rather than the most complete details. – Sotto Voce Aug 14 '22 at 17:01

1 Answers1

7

You can set the swappiness to 0.The Linux documentation says:

"This control is used to define how aggressive (sic) the kernel will swap memory pages. Higher values will increase aggressiveness, lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone.

The default value is 60."

Note that setting swappiness to 0 does not completely eliminate swap, it still swaps under conditions.

But is swap a problem? Just some swapped-out pages that are hardly used deserve to be swapped out, I think. To see if swap is a problem, run vmstat 60 for a while and look at the si and so columns. You'll probably see that they are 0 most of the time. Also, your column swpd will probably not change much. That would mean that the swapping is not a problem.

Is you have enough memory for what you are doing and you absolutely do not want any use of swap space for some exotic reason, swapoff the swap-space.

Note that, if you have system freezes, with these parameters, it is unlikely to be caused by swapping. Unless, of course four si and so columns in the vmstat output are high.

--edit--

Your vmstat output suggests that there are a few pages swapped out. That is not something to worry about; there is no big swapping going on. Certainly not enough to cause system freezes.

Ljm Dullaart
  • 4,643
  • I ran vm 60 10 and left my PC idle. Was I supposed to stress test it or something? – TypicalHog Aug 13 '22 at 15:50
  • 1
    @TypicalHog You should simply use your system as normal. If you don't see any swap-ins or -outs during normal use, the pauses and slowdowns you see are definitely not due to swapping because there was no swapping. (Even if you do see swapping, it's unlikely that the pauses you see are related to that, however, unless you have disk I/O issues.) ¶ Also, it's worth checking to see if swap-in events are counted only for actual swap space, or also for, e.g., loads of backing pages for mmap'd files, which can come from a filesystem. – cjs Aug 13 '22 at 23:13