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?
4 Answers
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?
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 of100
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!- 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.
- 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!
- 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.

- 3,783
- 2
- 25
- 38
-
I cant exactly remember what fixed the problem for me anymore, but I thank you for the well written answer that explain a lot. – inimene Sep 30 '14 at 19:11
-
According to your answer, I terminate some process so that less swap is used. Now the process
kwapd0
is gone. Thanks. – mtoloo Oct 22 '17 at 13:22 -
Consider your use case when using swap. If running Kubernetes, for example, official docs demand swap be disabled for proper functionality of the kublet service used for system management. – vhs Dec 16 '20 at 07:41
-
Changing the swappiness to 0 doesn't do anything.
kswapd0
still sometimes uses ridiculous amounts of CPU resources, after changing this setting and rebooting, even when there is available RAM and it is using 0 MB of the swap file. – Andrew Koster Jun 18 '21 at 20:03 -
1@AndrewKoster Interesting, I'd be sure to reboot your system and ensure the swappiness is still correct. If you want to guarantee swap is never used, I would turn off your swap partitions/files. Since your problem might be different that what is asked here (and considering this question is originally over 8 years old), you should open your own question with some memory stats of the problem you're seeing. – AlwaysTalkingAboutMyDog Jun 20 '21 at 06:17
-
1@AndrewKoster that's because the swappiness doesn't disable swap, it's determines how aggressive the kernel should be at preemptively swapping (swapping to virtual memory before running out of RAM). Setting it to zero means that it will not attempt swapping until RAM has run out. Some processes will always allocate virtual memory anyhow (Java processes in particular). The best way to disable swap is with
swapoff -a
. – Coder Guy Aug 20 '21 at 17:39 -
In most use cases (vast majority I would say, but who knows... ) , allowing swap will just cause troubles. Like servers I've seen die because of a badly behaved user or service. Without swapping the offending process is the first to be killed by the scheduler, at least is what happened in my experience. Once a long process activate swappiness, a server become unresponsive and would need in-person attention. The swap partition itself is useful for hibernation, though. – dawid Jul 21 '22 at 12:54
-
@dawid For the typical desktop user, swap can improve performance of their system if they are low on RAM. This depends greatly on the situation, but this question provides some information on that: https://askubuntu.com/questions/291378/do-we-still-need-swap-partitions-on-desktops
Keep in mind this question was in context to gaming on Ubuntu desktop, not managing an Ubuntu server. The concepts of swap remain the same, but the usefulness of swap depend on your situation. There are also better methods of limiting memory usage than relying on the OOM killer (e.g. cgroups).
– AlwaysTalkingAboutMyDog Jul 21 '22 at 23:03 -
I've upvoted your answer, but it has some flaws. Swappiness is now 0 to 200, not 0 to 100 (see my answer here, quoting straight from the Linux kernel source code), and your quote of
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).
is incorrect. – Gabriel Staples Dec 13 '22 at 08:25 -
@GabrielStaples Historically it has been 100 as you've mentioned, you might consider modifying Ubuntu's community wiki (https://help.ubuntu.com/community/SwapFaq). I'm wondering why they've changed it to 200 while keeping the default at 60...that seems unusual as it break/changes behavior. – AlwaysTalkingAboutMyDog Dec 14 '22 at 09:40
-
instead of rebooting, can do
sudo sysctl -w vm.swappiness=10
and then make it persistent with sysctl.conf – Donn Lee Dec 15 '22 at 21:32
kswapd0 runs at 99.9% of one CPU but is actually not swapping at all
To me it happens at times on Ubuntu 14.04 with kernel 3.19.0-50-generic (and earlier) running in a VMware vm. I have no clue, what made it appear, but it comes during idle time.
top
shows:
# top
top - 09:49:35 up 5 days, 18:35, 1 user, load average: 1.00, 1.00, 0.99
Tasks: 219 total, 2 running, 217 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 25.0 sy, 0.0 ni, 74.7 id, 0.2 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem: 3028784 total, 1874468 used, 1154316 free, 1010276 buffers
KiB Swap: 15624188 total, 3032 used, 15621156 free. 234928 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
52 root 20 0 0 0 0 R 99.7 0.0 122:15.21 kswapd0
3 root 20 0 0 0 0 S 0.3 0.0 0:29.86 ksoftirqd/0
7 root 20 0 0 0 0 S 0.3 0.0 9:49.47 rcu_sched
Temporary solution
a reboot solved the problem - temporarily.
following the answer on serverfault (kswapd often uses 100% CPU when swap is in use) there where the same settings on my system:
# cat /proc/sys/vm/swappiness
60
# cat /proc/sys/vm/vfs_cache_pressure
100
# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
the solution was actually running echo 1 > /proc/sys/vm/drop_caches
as root user:
# cat /proc/sys/vm/drop_caches
0
# echo 1 > /proc/sys/vm/drop_caches
# cat /proc/sys/vm/drop_caches
1
Or, as pointed out by theTuxRacer (thanks!), use the following command in case you are not logged in as root:
echo 1 | sudo tee /proc/sys/vm/drop_caches
now it's fine:
# top
top - 10:08:58 up 5 days, 18:55, 1 user, load average: 0.72, 0.95, 0.98
Tasks: 220 total, 1 running, 219 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 3028784 total, 681704 used, 2347080 free, 2916 buffers
KiB Swap: 15624188 total, 3032 used, 15621156 free. 81924 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9 root 20 0 0 0 0 S 0.3 0.0 14:10.40 rcuos/0
1 root 20 0 45652 8124 2888 S 0.0 0.3 1:54.98 init
Permanent solution (to be found)?
but since the actual reason is not yet known, and I did not fine any suitable explanation on the net, this is not a permanent solution. Actually, the selected answer could be the permanent solution. I just wanted to add this for future reference, as a reboot (to make sysctl take effect) is not always possible.
An other solution might be to set THP to either madvice
or never
(see poige's comment to his answer, How do I modify “/sys/kernel/mm/transparent_hugepage/enabled” and the referenced MongoDB Manual on Disable Transparent Huge Pages (THP))
cron job
i've set up the following batch as a cron job as a "permanent" solution:
#!/bin/bash
# Rev 2: Use ps instead of top
run as cron, thus no $PATH, thus need to define all absolute paths
cpu=$(/usr/bin/printf %.0f $(/bin/ps -o pcpu= -C kswapd0))
[[ -n $cpu ]]
&& (( $cpu >= 90 ))
&& echo 1 > /proc/sys/vm/drop_caches
&& echo "$$ $0: cache dropped (kswapd0 %CPU=$cpu)" >&2
&& exit 1
exit 0
invoked from root@localhost:~# crontab -e
with
# m h dom mon dow command
* * * * * /bin/bash /path/to/batch/drop_caches.sh >> /var/log/syslog 2>&1
Note: Above cron job script has been adapted to include Fredrik Erlandsson's suggestion: A much simpler and more efficient way to determine kswapd0's CPU usage. Thanks!

- 615
- 5
- 6
-
Very nice answer, thanks. RPi kernel updated and this is what I get, berserking kswap. – Paul B Jun 01 '16 at 19:27
-
Thanks, @PaulB. I've added to my answer the cron job I use as a permanent solution on my system. – Martin Rüegg Jun 03 '16 at 07:07
-
As correctly pointed out by @Veger, this also works on 16.04. As I'm currently using myself. So added the tag. Thanks! – Martin Rüegg Nov 21 '16 at 11:21
-
Thanks again, @Veger! - I've corrected the missing exclamation mark in the script's Sha-Bang. – Martin Rüegg Dec 02 '16 at 19:39
-
2"echo 1 > /proc/sys/vm/drop_caches" fixed the high CPU usage for me--night and day difference! kswapd0 went from 100% CPU to 0%. An explanation for why and a permanent solution would be great. (Side note: I'm running linux kernel 4.8.0-36-generic with 16 GB mem and 16 GB swap.) – josephdpurcell Jun 12 '17 at 17:14
-
Hi josephdpurcell, thanks for your comment. I don't know the reason and my permanent solution is running the cron job as shown in the answer. Hope that helps. – Martin Rüegg Jun 13 '17 at 10:26
-
This solution is valid today (2017) for UBUNTU 16 LTS? Is not a Kernel bug? And this other solution, must ignore? – Peter Krauss Nov 06 '17 at 19:02
-
2I would suggest changing
(( $cpu >= 90 )) \
to(( $(printf %.0f \
/bin/ps -o pcpu= -C kswapd0`) >= 90 )) \ ` and removing everything above.. – Fredrik Erlandsson Jan 08 '20 at 21:59 -
2@PeterKrauss, nowadays running Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-74-generic x86_64) and 20.04 LTS (GNU/Linux 5.4.0-21-generic x86_64) I have not experienced this anymore and am not running the cron job mentioned in this answer. – Martin Rüegg May 19 '20 at 09:23
-
1
echo 1 | sudo tee /proc/sys/vm/drop_caches
in case you are not logged in as root and you have a sluggis terminal. – theTuxRacer Jun 25 '20 at 15:27 -
A more permanent solution is to increase the size of your swap file.
kswapd will take 100% CPU when searching for swap space that doesn't exist (if your swap partition is full). htop
will show if your swap space is full at the top of the screen or you can use the free -h
command to check used swap.
After using the following commands to increase the size of my swap file from 4GB to 8GB, kswapd now runs at 0% CPU with swappiness set to 60*.
sudo swapoff /swapfile
sudo rm /swapfile
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Click here for a more detailed description of these commands
This assumes that you are already using a swap file (default Ubuntu setup) and that the /swapfile
is referenced in /etc/fstab
. Also, your swap file may be larger or smaller depending on how much RAM you have so adjust accordingly.
* It should be noted that I use a SSD so disk performance is not an issue. However, swap is almost always a good thing if you're running out of RAM.
See this related answer if you're having problems like excessive swap paging and system freezing when the system has been running for a day or more without a reboot. This problem has very similar symptoms but it has a different cause. Therefore, if the problem comes back, you may need to try this alternative solution.

- 43,546
- 8
- 97
- 150
-
2"kswapd will take 100% CPU when searching for swap space that doesn't exist" : indeed, for instance when your swap partition is not mounted ! ^^ – Skippy le Grand Gourou Oct 02 '21 at 14:56
Malware running as guest
If you have ever enabled Ubuntu's guest account and later enabled SSH, you may have malware running using your guest account.
sudo find /home -f kswapd0
We have been finding it under /home/guest/.configrc/
Many people are finding this question today on machines that require no swapping, and seeing that their available memory is normal, only to find out mining software has been installed and is running under the guest account, even automatically on startup.
Joining any network (conference, cafe, city) that has a compromised machine, or if you use a service like ngrok
, all while having SSH open on your system leaves your computer exposed to this simple guest vulnerability.
An entry dedicated to this problem is here: CPU 100% with kswapd0 process, although no swap is needed

- 191
- 3
-
1This happened to me as well, but it was on a cloud server I hosted. You can find more information about this malware here: https://yoroi.company/research/outlaw-is-back-a-new-crypto-botnet-targets-european-organizations/ I also found this short article on how to remove it: https://nitifilter.com/en/ive-been-hacked/ – SapuSeven Dec 05 '22 at 19:22
swapoff -a
. Run it as a superuser. – vhs Dec 16 '20 at 07:57/temp/.Xsomething
and~/.ssh/authorized_key
has been altered. It made cloud server to shut down. Check out the link above, it has serious analysis about the problem. If your computer starts acting weird suddenly, I think you better watch out for cryptominers or viruses. – KH Kim Aug 09 '21 at 16:15