1

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:

  1. disable swap
  2. have a cron running every 1 minute to check how much free RAM there is
  3. 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?

Nuno
  • 461
  • 1
  • 5
  • 23
  • 1
    Check `cat /proc/sys/vm/swappiness` to be sure. Some distros have a habit of resetting it (but CentOS typically isn't one of them). Would it be possible that the RAM was briefly consumed then released, leaving some data moved to swap - did you run `mysqltuner` to show the maximum theoretical usage of mysql? – tater Oct 23 '20 at 10:29
  • @tater Thanks. The result of the command is "1", as expected. Yeah, I suppose one possibility is that something may take a lot of RAM and then release, but I haven't caught that happening yet. However, the scenario above does happen often, slowly over time. The database itself is 'fine' (well tuned/configured, and regularly checked). The server runs other things beside MariaDB, however. – Nuno Oct 23 '20 at 10:40
  • Why do you think this is a problem? Swapping out parts of processes that aren't used so that memory can be used for other things (like caching!) is normal and generally welcome behavior. You are probably losing performance by forcing everything out of swap. – Michael Hampton Oct 23 '20 at 17:58
  • @MichaelHampton - in my question above, I mention about the system performing poorly. Once the swap is moved into actual RAM, performance seems to improve. – Nuno Oct 23 '20 at 19:11

1 Answers1

1

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.

CA_Tallguy
  • 101
  • 3
  • Thank you for your suggestions. The server is physical & dedicated. Interesting stuff about NUMA -- I'll have a look! – Nuno Dec 15 '20 at 12:57