Most of the time when my computer starts to need swap, I see a massive spike in CPU usage (kswapd0 is consistently using 99%-100% CPU). According to top, the time is spent in sy (system/kernel) not wa (IO wait).

I am running Linux 4.0.4-2-ARCH on a C720 with 2GB RAM, and 6GB swap on an SSD.

I seem to have this problem with or without discard pages (TRIM) turned on.

Are there any setting I should inspect or tweak to see if I can fix this?

Is there any way to debug the problem? Something like strace for kernel threads?

Running with default Arch Linux settings:

/proc/sys/vm/swappiness = 60
/proc/sys/vm/vfs_cache_pressure = 100
/sys/kernel/mm/transparent_hugepage/enabled = [always] madvise never

  • take a look if irqbalance is running, and see /proc/interrupts if interruptions are well balanced. – fgbreel Jun 2 '15 at 17:57
  • @fgbreel: irqbalance is not running. /proc/interrupts seems more or less balanced. The problem really only affects 1 CPU at a time. – Zaz Jun 2 '15 at 18:16
  • Install and start irqbalance service and keep watching to see if the time will be more distributed across the all CPU cores. – fgbreel Jun 2 '15 at 18:22

It seems a relatively common problem

When the problem is happening, can you check if issuing the following command stops it: echo 1 > /proc/sys/vm/drop_caches

If it works, you can schedule it as a periodic cron job as a workaround.

| improve this answer | |
  • 1
    Unfortunately that has no effect, and /proc/sys/vm/drop_caches stays at 1. – Zaz Jun 2 '15 at 21:21
  • 3
    This works and is very helpful, but what is it doing? I am on a very low resource machine and this issue comes up all the time (and makes it hard to use my computer over ssh) but I do not know what is going on. Scheduling this as a cron job seems like a said way to fix the glitch... – Startec Aug 31 '16 at 5:15
  • 1
    The above command drops the cached information that Linux pagecache subsystem keep to speedup I/O access. For example, when you read a file from the disks, it is also stored on the pagecache to speedup further access to file content. By dropping caches, you decrease memory pressure and this seems sufficient to avoid the kswapd problem. – shodanshok Aug 31 '16 at 9:19
  • This 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:13
  • See enhanced solution at askubuntu.com/a/736956/439867 – Peter Krauss Nov 6 '17 at 19:04

I have a C720 running Linux Kernel 4.4.0 on Ubuntu 14.04.1 LTS with 2 GB RAM and 2 GB swap.

Assuming heavy Chrome/Chromium usage, here are some ways to make your system more performant:

  1. Edit /etc/default/grub and add the following kernel parameters to the GRUB_CMDLINE_LINUX_DEFAULT line:
    • elevator=noop
    • zswap.enabled=1
    • transparent_hugepage=madvise
  2. Run sudo update-grub2.
  3. Edit /etc/sysctl.conf and append the following:
  4. Reboot.

You can verify the changes like so:

$ dmesg | grep -i noop
[    0.694680] io scheduler noop registered (default)
$ dmesg | grep -i zswap
[    0.724855] zswap: loaded using pool lzo/zbud
$ cat /sys/kernel/mm/transparent_hugepage/enabled
always [madvise] never
$ sysctl vm.swappiness
vm.swappiness = 25
$ sysctl vm.vfs_cache_pressure
vm.vfs_cache_pressure = 1000


Increasing vm.min_free_kbytes in step #3 may be beneficial. Try a value of 131072 (128 MB). The final takeaway is that Linux on the desktop doesn't perform very well in low-memory situations. Some have suggested placing Chrome/Chromium in a cgroup, but that's beyond the scope of this answer.

| improve this answer | |

kswap kernel are used to allocate and release momery pages, if your swap are used in you see this kernel threads using so much cpu time, that would say the kswap kernel threads are scanning the memory pages for swaping some pages and serve the memory allocation request.

I think drop the cache in this case doesn't help, because the kernel reclaim the cache when the OS is tight memory situation automaticaly.

If you don't have any memory problem and use the free command, you will se so much memory used as cache, but if you have a memory problem, Linux reduce the cache for serve the memory allocation requests, without any need to drop cache

you can use sar -B and looking for majft and pgscank values, for other values man sar

| improve this answer | |
  • 1
    What are sar majft and pgscank? I couldn't find them on my machine or in my package manager (pacman). – Zaz Jun 2 '15 at 23:10
  • free shows 1.0G used, 545M cache, 425M available. Seems pretty normal to me. – Zaz Jun 2 '15 at 23:11
  • what did you see in sar -B? – c4f4t0r Jun 3 '15 at 6:47
  • zsh: command not found: sar – Zaz Jun 3 '15 at 14:59
  • @Zaz You need to install it then. – Thorbjørn Ravn Andersen Jan 31 '18 at 10:12

(This is quasi-answer -- too long to be a comment, but not a ready answer though)

1) What about using not 6G but less, say 1 or 2 GiBs (you can set-up the size with mkswap w/o resizing swap partition) — tried? What results?

2) What's sysctl vm.swappiness, sysctl vm.vfs_cache_pressure?

3) What's cat /sys/kernel/mm/transparent_hugepage/enabled?

N. B. Do you realize you're gonna wear out your SSD significantly in that kind of set-up (not that much RAM, huge swap).

P. S. I could recommend trying to use UltraKSM but it requires patching a kernel. I do have some builds of mine own (-realtime and BFS based), but they're for .deb-based systems and meanwhile they can be used on different systems quite easily (usually you would need just to unpack the .debs and make corresponding initrd/initramfs, it can be a hassle for people not that familar with that side of Linux)

(to be continued)

| improve this answer | |
  • I added the information to my question. Arch Linux has an AUR package for building Linux with UltraKSM, so I might try that. Thanks for the help! – Zaz Jun 2 '15 at 18:57
  • 1
    Regarding SSDs, I think their fragility compared to HDDs is often exaggerated; even with flat out writing and erasing, it can take years to wear out an SSD. Even if I'm wrong and wear my SSD out, I'll should still be able to recover my data, and will only have lost a $30 SSD. – Zaz Jun 2 '15 at 18:58
  • ok, try changing hugepages → madvise, swappiness → 0, vfs_cache_pressure → 5000 – poige Jun 3 '15 at 0:54
  • Doesn't seem to make any difference. Sorry. – Zaz Jun 3 '15 at 15:43
  • Did you do 1st item in the list above? – poige Jun 3 '15 at 17:32

If you have a service running inside of docker like puppeteer (chrome headless api) Inside your dockerfile add the dumb-init.

If running Docker >= 1.13.0 use docker run's --init arg to reap zombie processes

docker run container --init

If you are running a <=1.13.0 in docker use dumb-init. Add this to your Dockerfile.

ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.0/dumb-init_1.2.0_amd64
RUN chmod +x /usr/local/bin/dumb-init
ENTRYPOINT ["dumb-init", "--"]
| improve this answer | |

I am not sure why this answer has not been suggested: killall -9 kswapd0

I came across this problem where the kswapd0 process was running as a non-root user who had not logged in for a long while. I have killed this process and the issue hasn't returned.

No, this does not address the root issue (how did it even get 100% in the first place) but allows you to quickly recover usage of the system.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.