Linux Ubuntu kswapd0 High CPU Usage

This morning I noticed that a website on our test server hosted on Digital Ocean is not accessible. After a short investigation, I saw that the 50% - 60% of the cpu was used by a process called kswapd0.

Digital Ocean Droplet CPU Graphic

To check this, I learned that I can use the "top" command on the server. As I can see in the output of this command, a process named kswapd0 uses a high amount of CPU and as seen in the panel above, as a result of near 100% CPU usage, our http server was also not working properly as expected.

$ top

After searching a bit, I learned that the process called kswapd0 runs when memory is limited and to perform a process called swapping. As far as I understand it, it's a system that uses the physical disk instead of using the RAM when memory is limited. The kswapd0 process is also managing this virtual memory.

Is kswapd0 a virus?

As far as I have learned from my research, there is most likely no virus or malware related to kswapd0. However, some comments mention that there can be a crypto miner if it is in a folder named a/ among user files such as .rsync or .configrc. You may want to check using the following command or manually.

$ sudo find /home kswapd0

How to solve high cpu due to kswapd0?

In our case, there was no infiltration. Therefore, I continued to investigate how this swap operation took place and learned about concepts called swappiness and swap space. While the swappiness value determines when will the system perform this operation, the swap space is about how large this system will be using a specified file. At this point, I found a guide that I can follow on Digital Ocean.

Also high memory usage on Mysql

After I setup swap, the high usage of kswapd0 was no more. However, while investigating kswapd0, I saw that mysql also uses relatively high memory.

Top command showing high mysql usage

As a result of a quick search, I found the following settings that turn off the performance schema feature which is said that can be turned off on servers with low RAM:

$ nano /etc/mysql/my.cnf

// Add to bottom
[mysqld]

performance_schema = OFF

After this, together with swap, the usage on our server have returned to normal levels.

I hope it stays that way :)