kswapd0 is taking a lot of cpu
kswapd0 is taking 99.9% of my CPU as top shows me, no any other process consume more CPU then this process.
1. Tell kswapd0 to only move stuff to SWAP when you are completely OUT of RAM. Run below command to avoid this SWAP issue.
echo vm.swappiness=0 | sudo tee -a /etc/sysctl.conf
Meaning of this command is if your RAM is fully consume then after it will used the SWAP partition which is created on your HDD.
2. A reboot can resolved the problem temporarily.
3. Check output of below command
# cat /proc/sys/vm/vfs_cache_pressure
100
The solution for dropping cache is
the solution was actually # echo 1 > /proc/sys/vm/drop_caches
:
4. For permanent solution you can create shell script and set it in cron.
#!/bin/bash
## run as cron, thus no $PATH, thus need to define all absolute paths
top=/usr/bin/top
grep=/bin/grep
top=$($top -bn1 -o %CPU -u0 | $grep -m2 -E "%CPU|kswapd0")
IFS='
'
set -f
i=0
for line in $top
do
#echo $i $line
if ! (( i++ ))
then
pos=${line%%%CPU*}
pos=${#pos}
#echo $pos
else
cpu=${line:(($pos-1)):3}
cpu=${cpu// /}
#echo $cpu
fi
done
[[ -n $cpu ]] &&
(( $cpu >= 90 ))
&& echo 1 > /proc/sys/vm/drop_caches
&& echo "$$ $0: cache dropped (kswapd0 %CPU=$cpu)" >&2
&& exit 1
exit 0
Invoked with
# m h dom mon dow command
* 4 * * * /bin/bash /path/to/batch/drop_caches.sh >> /var/log/syslog 2>&1
This command will run at every 4 hours so set timings as per your need.