You are not logged in.

#1 2014-02-16 16:32:16

Lockheed
Member
Registered: 2010-03-16
Posts: 1,524

[solved] Kill process if it exceeds X of virtual memory

I am using checkgmail and it has a serious memoryleak problem. After few hours of running, its memory usage goes from initial 50M, to 500M, to 2000M, and does not stop there.

So I am trying to have it killed and restarted if it exceeds, say, 200M.

What would be the best way of doing that?

Last edited by Lockheed (2014-02-16 17:29:02)

Offline

#2 2014-02-16 16:35:49

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Kill process if it exceeds X of virtual memory

You can periodically (e.g. every 5 minutes) check if the set condition has been met and display a warning or kill the process.

Offline

#3 2014-02-16 16:55:46

Lockheed
Member
Registered: 2010-03-16
Posts: 1,524

Re: [solved] Kill process if it exceeds X of virtual memory

Yes, I know, but I want to automate it rather than do it manually.

Offline

#4 2014-02-16 16:57:16

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Kill process if it exceeds X of virtual memory

$ ps -C urxvtd -o rss=
42516
$ if [ $(ps -C urxvtd -o rss=) -gt "40960" ]; then echo 'KABOOM!'; fi
KABOOM!

urxvtd uses over 40MB RAM [1], so it exceeds the threshold I've set up.
Depending how often you want to check it, you can use cron or add a 'sleep' timer.


[1] I'm using just RSS http://en.wikipedia.org/wiki/Resident_set_size, but you can measure e.g. vsize too.
http://publib.boulder.ibm.com/infocente … ine_ps.htm

Offline

#5 2014-02-16 17:07:32

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: [solved] Kill process if it exceeds X of virtual memory

Use ulimit, I suppose.

Offline

#6 2014-02-16 17:11:03

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Kill process if it exceeds X of virtual memory

brebs wrote:

Use ulimit, I suppose.

I did read 'man ulimit' (man 1p ulimit), but it doesn't mention memory and stuff like setrlimit etc. seems a bit hard to grasp.

Offline

#7 2014-02-16 17:28:43

Lockheed
Member
Registered: 2010-03-16
Posts: 1,524

Re: [solved] Kill process if it exceeds X of virtual memory

@karol
That worked. Adding & at the end makes it work in cron, too.
Thanks!

Offline

#8 2014-02-16 17:36:47

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [solved] Kill process if it exceeds X of virtual memory

You can extend it to log e.g. the time and date when it killed the process.
Are you restarting checkgmail automatically after you kill it?

Offline

#9 2014-02-16 17:39:28

Lockheed
Member
Registered: 2010-03-16
Posts: 1,524

Re: [solved] Kill process if it exceeds X of virtual memory

Yes. Instead of "echo", I run this script if memory is exceeded:

#!/bin/bash
killall checkgmail
sleep 3
DISPLAY=:0 /usr/sbin/checkgmail-gtk

I don't really care about logging it, though.

Offline

Board footer

Powered by FluxBB