Issue
Recently, a Linux server has been experiencing high CPU usage, causing the server to become unresponsive and other processes to fail to execute smoothly. By using the top command to check resource usage, it can be seen that the kswapd0 process is consuming a large amount of CPU resources.
Cause and solution
Based on my research, the kswapd0 process is responsible for managing virtual memory.
Typically, a Linux system comprises RAM, swap, and EXT4 components. The EXT4 partition is used to store regular files and can be created on either an HDD or an SSD, though it is relatively slower compared to RAM. RAM, commonly referred to as memory, is used for high-speed program execution. The swap partition, on the other hand, is used as additional virtual memory and is normally allocated on physical disks, especially on machines with limited physical RAM.
The swap partition acts as virtual RAM, generally located on HDD/SSD. When physical memory is limited, the system might frequently face memory shortages, causing it to use the swap partition to temporarily store data from the physical memory. When available physical memory is low, the kswapd0 process moves less frequently used programs to the swap partition, which can result in these programs becoming unresponsive. For instance, if a machine has 4 GB of RAM and needs to run a program requiring 5 GB of memory, at least 1 GB will be placed in the swap partition. The process of moving memory handled by kswapd0 can lead to significant CPU usage. To address this issue, there are several solutions:
1 modify /etc/sysctl.conf file
echo vm.swappiness=0 | sudo tee -a /etc/sysctl.conf
Here, 0 represents the percentage of remaining physical memory. The value ranges from 0 to 100, and setting it to 0 means that kswapd0 will only execute when there is no physical memory available.
2 Close some unused programs.
3 Add more memory.
4 Increase the swap partition.
Summary
This article primarily discusses the issue I encountered with kswapd0 using excessive CPU resources and how to resolve this problem.