开发者

Finding out memory footprint size

I would li开发者_C百科ke to be able to restart a service when it is using too much memory (this is related to a bug in a third party library)

I have used this to limit the amount of memory that can be requested:

resource.setrlimit(resource.RLIMIT_AS, (128*1024*1024, 128*1024*1024))

But the third party library gets stuck in a memory allocation busyloop failing and re-requesting memory. So I want to be able to, in a thread, poll the current size of the memory of the process.

Language I'm using is python, but a solution for any programming language can be translated into python code, provided it's viable and sensible on linux.


Monit is a service you can run to monitor external processes. All you need to do is dump your pid to a file for monit to read. People often use it to monitor their web server. One of the tests monit can do is for total memory usage. You can set a value and if your process uses too much memory it will be restarted. Here's an example monit config

check process yourProgram
       with pidfile "/var/run/YOUR.pid"
       start program = "/path/to/PROG.py" 
       stop program = "/script/to/kill/prog/kill_script.sh"
       restart if totalmem is greater than 60.0 MB 


This is the code that I came up with. Seems to work properly, and avoids too much string parsing. The variable names I unpack come from proc(5) man page, and this is probably a better way of extracting the OS information than string parsing /proc/self/status.

def get_vsize():
    parts = open('/proc/self/stat').read().split()
    (pid, comm, state, ppid, pgrp, session, tty, tpgid, flags, minflt, cminflt,
     majflt, cmajflt, utime, stime, cutime, cstime, counter, priority, timeout,
     itrealvalue, starttime, vsize, rss, rlim, startcode, endcode, startstack,
     kstkesp, kstkeip, signal, blocked, sigignore, sigcatch, wchan,
    ) = parts[:35]
    return int(vsize)

def memory_watcher():
    while True:
        time.sleep(120)
        if get_vsize() > 120*1024*1024:
            os.kill(0, signal.SIGTERM)


You can read the current memory usage using the /proc filesystem.

The format is /proc/[pid]/status. In the status virtual file you can see the current VmRSS (resident memory).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜