Ubuntu – kswapd0 is taking a lot of cpu


kswapd0 is taking 99.9% of my CPU as top shows me, the problem appeared today when gaming and first time it went away after 6 minutes and now it has been doing it for about 20 minutes. How is this fixable and what is causing this?

Best Answer

The process kswapd0 is the process that manages virtual memory. Your machine should have RAM, SWAP, and the EXT4 on your HDD/SSD. The ext4 is where everything is stored, and it is always slower to access than RAM. RAM is like a half-way running space for programs to access information quickly. Most computers have at least 4GB of RAM, which under normal conditions is plenty. When playing a game, however, you may run low on RAM space, which is where SWAP comes in.

SWAP is a fake RAM located on your HDD/SSD next to your EXT4. It is quicker to access than the EXT4, but it is much slower than actual RAM. When you run low on memory, kswapd0 moves programs that you are not using/not using as much as other programs to the SWAP, which causes extreme lag on those processes. If your game was needing 5GB RAM, 1GB at LEAST would be in SWAP. That means when it tries to access that information, it has to wait longer to get it.

This entire process causes extreme CPU usage, moving information from and to SWAP and RAM and handling the request of information all at the same time. How to solve this issue?

  1. Tell kswapd0 to only move stuff to SWAP when you are completely OUT of RAM. This is the single most effective method to resolving SWAP issues. Run

    echo vm.swappiness=0 | sudo tee -a /etc/sysctl.conf

    where 0 is the percent left out of 100 at which SWAP should be used (when you have 0% RAM left, SWAP will start taking in data). You can also just edit /etc/sysctl.conf to your liking instead of adding this command to the end of it everytime using gedit or nano or whatever, be sure to sudo though, this file is root owned. Reboot and your are set!

  2. Reduce the consumption of RAM by other processes or close other programs while running high memory programs. This is why most games tell you to close all other windows before playing, or installations do the same. Things like file syncing services tend to take a lot of memory.
  3. Buy more RAM. Installing RAM is not as hard as it sounds. One or two screws on a small compartment (if you are on a laptop) and a simple click. Just be sure you are buying the correct kind!
  4. Lower processes of the CPU much as you did with the RAM. This will help those RAM to SWAP bursts to go much smoother.

That's the best that you can do. Others may say disable swap completely, but that is dangerous and I would NOT recommend that. That can cause entire systems to freeze up if there is a memory leak or too many applications running. Just realize that the SWAP is a failsafe for the RAM. It is definitely not as fast or efficient as RAM, but it's better than Window's Pagefile! (which accomplishes the same purpose)

EDIT: If you are interested in learning more about SWAP, see here.