a short script for taming memory hogs

Other talk about Salix
Post Reply
GJones
Donor
Posts: 300
Joined: 22. Jul 2011, 23:27

a short script for taming memory hogs

Post by GJones »

You've all probably seen cases where Firefox goes bananas on a poorly designed website, spilling over into swap space and bringing your computer to its proverbial knees... Here is a simple script for those situations.

Code: Select all

#!/bin/bash -
[ $# -ne 2 ] && {
  echo "Usage: $(basename $0) <scan_interval> <max_oom_score>"
  echo "Example: $(basename $0) 3 100"
  exit
}
INTERVAL=$1
MAX_SCORE=$2
while sleep $INTERVAL; do
  for task in /proc/*; do
    [ -e $task/oom_score ] && read val < $task/oom_score
    [ $val -gt $MAX_SCORE ] && kill -STOP $(basename $task)
  done
done
It's actually inspired by this Python script:
https://github.com/tobixen/thrash-protect
but is simpler and dumber. It scans /proc for processes that look like memory hogs, and suspends their execution. If it gets the wrong program, you can resume it with 'kill -CONT', otherwise you can terminate the hog normally (as opposed to forcing a reboot, or logging in remotely).

The script takes two arguments, a scanning interval and a maximum OOM killer score. Scanning interval is (obviously) how often it scans /proc. The OOM score is a little more iffy. The score is roughly equal to a program's resident set size plus its swap usage, as a percentage of available RAM and swap; multiplied by 10, i.e.
(% resident memory + % swap usage) * 10
As detailed here:
http://serverfault.com/questions/571319 ... 326#571326

So, a good value for OOM score may be higher on machines with less memory.

(Or possibly not, if you use a multi-GB swap partition even on your Pentium 4 PCs. Still not certain yet.)

Note: running this script as root is probably inadvisable... If you're willing to test it, probably better to invoke it from .xsession or suchlike.
tessi
Posts: 10
Joined: 22. Mar 2015, 08:59

Re: a short script for taming memory hogs

Post by tessi »

thanks
Post Reply