GNOME shell has been leaking memory for a while hurting user experience. For instance, it could freeze a laptop sending it to deep swapping. To counteract, we had to detect the situation, switch to the virtual terminal, login and kill the offending process. Luckily, restarting gnome-shell is seamless and harmless. I have just realized that monitoring it could easily be automated.

First, we create the action ~/bin/


# Kill any gnome-shell process consuming more than 300 Mb of RAM
ps -A --sort -rss -o comm,pid,rss | grep '^gnome-shell\s' | awk '{print $2, $3}' | \
    while read -r pid rss; do
        [[ "$rss" -gt 300000 ]] && kill -KILL $pid || true

Then we create a one-shot user service ~/.config/systemd/user/check-gnome-shell.service:

Description=Check whether gnome-shell doesn't leak memory

ExecStart=/bin/bash /home/sakhnik/bin/

Then comes the timer ~/.config/systemd/user/check-gnome-shell.timer:

Description=Check whether gnome-shell is leaking memory



And finally, we enable and start the timer:

systemctl --user enable check-gnome-shell.timer
systemctl --user start check-gnome-shell.timer

And that’s all. Every 5 minutes the action is executed. It checks RSS of gnome-shell processes, and kills those occupying more than 300M.