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.
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 aswapoff 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.