I have: vm.swappiness = 1
as suggested here to prevent using Swap when there is plenty of available memory.
(0 would completely disable swap, as far as I understand)
In frequent cases, I end up with, say, 5GB swap used (total 16GB),
while I have 9GB free available memory (total 32GB)
(only 1GB free if we count cached RAM).
As my application starts to get slow (since most of the Swap is from MariaDB), I run:
swapoff -a
swapon -a
And all the swap memory is moved to the main RAM.
I end up with 5.6GB available RAM, 4GB if we count cached.
There is no apparent reason to have so much Swap.
I've been researching about this, and I cannot find a way to prevent using Swap when it's not needed.
vm.swappiness = 1
is definitely "permanently saved", since it is on the sysctl.conf file, and there have been reboots, etc.
I don't want to disable swap, but the only workaround I can think of is:
- disable swap
- have a cron running every 1 minute to check how much free RAM there is
- enable swap if available RAM is low
But, this is VERY risky to do, and I wish the OS was smart enough to manage this better!
Is there absolutely no way to only use swap when absolutely needed?
Do you have lot of stuff running on the server? If so, maybe consider a dedicated machine or VM for the DB.
Is this physical or VM/cloud server? Some cloud providers do NOT want you to use swap at all (so just putting that out there in case it is relevant): https://docs.rackspace.com/support/how-to/swap-space-on-cloud-servers/
There is some good discussion on this page for troubleshooting swap for MySQL or MariaDB -- maybe check the NUMA section toward the end: https://fromdual.com/do-not-underestimate-performance-impacts-of-swapping-on-numa-database-systems
This Q&A at MariaDB site leads to NUMA interleaving settings as a possible solution: https://mariadb.com/kb/en/swap-usage-wont-decrease/
A lot of discussion about memory and MariaDB: https://mariadb.com/kb/en/mariadb-memory-allocation/
And this is the authoritative youtube video for NUMA :-D https://youtu.be/KmtzQCSh6xk
Please upvote/accept this answer if it is helpful. Thanks.