How to get the max memory usage of a program using psutil in Python

后端 未结 2 722
无人及你
无人及你 2020-12-03 20:04

I am using the following code to get the max memory usage of the program.

    import os, subprocess , psutil
    def mem(cmd):
        try:
            with          


        
2条回答
  •  醉梦人生
    2020-12-03 20:47

    means that the subprocess is a zombie process (it is dead but its status has not been read yet by the parent (p.poll() or p.wait())). It seems both psutil and ps shows RSS to be zero for such processes.

    The result depends on whether the subprocess will exit sooner than p.memory_info() is called. It is a race. If you add a delay at the exit in the C++ program then p.memory_info() may be called before the subprocess exits and you should get non-zero results.

    The problem is that I can get arbitrary programs to evaluate . The language is also not fixed. Isn't there an elegant solution to this problem?

    You might need OS support to save info about the memory usage by a subprocess even after it exits. Or you could run the program using a memory profiler such as valgrind and read its results. To collect results:

    $ valgrind --tool=massif cmd arg1 arg2 
    

    To see the results, you could use ms_print:

    $ ms_print massif.out.* | less
    

    Or GUI Massif-Visualizer

    @mdadm suggested a simpler solution: time command:

    from subprocess import Popen, PIPE
    
    p = Popen(['time', '-f', '%M'] + args, stderr=PIPE)
    ru_maxrss = int(p.communicate()[1])
    print("Maximum rss %d KB" % ru_maxrss)
    

    GNU time uses wait3() to populate resource usage info if it is available. It can be called in Python:

    import os
    from subprocess import Popen
    
    p = Popen(args)
    ru = os.wait4(p.pid, 0)[2]
    print("Maximum rss %d KB" % ru.ru_maxrss)
    

    I've compared the maximum value returned by psutil.Process.memory_info (rss) with ru_maxrss value returned by os.wait4 and with the maximum total memory reported by valgrind --tool=massif: they are similar.

    See also:

    • A way to determine a process's “real” memory usage, i.e. private dirty RSS?
    • How is memory usage reported in Linux?
    • Find maximum memory consumed by a process specified at the command-line.

提交回复
热议问题