5

My app has several JVMs running in Linux and swap usage is mostly zero. However, sometimes by high traffic or slowness of database, JVMs consume swap memory, which is not recommended. If it happens, my plan is to recycle swap by 'swap off/swap on' without app shutdown at night window.

When I swap off on OS, what happens to processes that consume swap memory? Are they losing allocated memory blocks? Or does the OS move swap memory blocks back to physical memory? I'm asking under the assumption that physical memory has enough free space to take all swap usage.

The reason that I'm searching for recycling swap is because of swap usage behavior; processes are not consuming swap when swap usage is zero, but they start consuming it more and more when swap usage is non-zero even when there are plenty of physical memory available.

My question is dealing with actual practice when a server hits heavy demand and uses swap memory, and never goes back after heavy demand is gone.

Zanna
  • 69,223
  • 56
  • 216
  • 327
Won
  • 159
  • 1
  • 4
  • 2
    Why do you swap off/ swap on? When extra memory is needed, OS will use it. When it is not needed, it will not be used. There is absolutely no reason to do swap off/swap on, just let OS handle it. Swap is a safety net when you are short on memory, otherwise out-of-memory kill will start killing processes or OS can go to kernel panic. – marosg Jun 18 '20 at 13:54
  • @marosg I added it to question. OS behavior on swap usage is not same as you described. Once swap usage becomes non-zero, OS keeps utilizing it even there are physical memory available, so it won't never go back to zero until recycle. – Won Jun 18 '20 at 14:18
  • You can set swappiness instead of "recycling swap". – Pilot6 Jun 18 '20 at 14:19
  • If there is enough RAM, active programs won't use swap anyway. Your ideas are wrong. – Pilot6 Jun 18 '20 at 14:20
  • @Pilot6 swappiness might work but my question is what value of swappiness match my needs? 0 means no swap at all, which is not feasible to me. With non-zero value even 1, I'm not sure if the server drops all swap usage when free physical memory is available. – Won Jun 18 '20 at 14:57
  • See https://www.howtogeek.com/449691/what-is-swapiness-on-linux-and-how-to-change-it/ – Pilot6 Jun 18 '20 at 15:36
  • 1
    Your description describes normal RAM/swap behavior. Edit your question and show me `free -h`. – heynnema Jun 18 '20 at 15:53

1 Answers1

11

I would just trust GNU/Linux for the management of the memory.

When memory demands are high, GNU/Linux indeed will swap inactive processes to swap memory to increase available physical memory (RAM) for running processes and for file cache. Even when memory demands decrease, that swap memory may not be released back to RAM if there is no need. This way, the available memory is at max for the processes that actually need it, and for file buffers. The inactive processes keep sitting away in the swap.

Is 'swap off' safe when app is running on production?

Yes, even when there would be not enough RAM to take all swap usage. If there is enough RAM next to the current needs, turning off the swap will succeed. If there is not enough physical memory, swapoff will not succeed and issue a swapoff failed: Cannot allocate memory error. So also there, thanks to system design, it remains safe.

However, again: it is not recommended to proceed this way: it will not provide any performance gains. In contrast, it will result temporarily in less available RAM for running processes and file caches, because part of it is actually wasted to reallocate parts to RAM that were not in active use/needed anyway.

vanadium
  • 82,909
  • 6
  • 116
  • 186