如何在 Python 中使用 psutil 获取程序的最大内存使用量 [英] How to get the max memory usage of a program using psutil in Python

查看:44
本文介绍了如何在 Python 中使用 psutil 获取程序的最大内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来获取程序的最大内存使用量.

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

    import os, subprocess , psutil
    def mem(cmd):
        try:
            with open('in.txt','r') as infile, open('out.txt', 'w') as outfile:
                p=psutil.Popen("./"+cmd,shell=False,stdin=infile,stdout = outfile)
            print p.memory_info()
        except Exception:
             print "Error"
    cmd=raw_input()
    mem(cmd)

问题有时出在程序的初始运行中,内存使用输出为 (0,0),但随后显示正确的输出.我不知道为什么会发生这种情况.对于某些程序,例如 C++ 中的 hello world 程序,输出为 pmem(rss=4096, vms=315392) 大约 0.3M(我认为输出以字节为单位)但运行 hello worldideone.com 中的程序给出的输出为 ~3M.为什么会出现这种差异?

The problem is sometimes for initial runs of the program the memory usage output is (0,0) but subsequently it displays the correct output. I dont why this happening. For some programs such as the hello world program in c++ the output is pmem(rss=4096, vms=315392) which is about 0.3M( I think the output is in bytes ) but running the hello world program in ideone.com gives the output as ~3M. Why is there this disperency?

cmd 是可执行文件的名称.

cmd is the name of the executable.

命令的输出 print subprocess.check_output(['ps', 'v', '-p', str(p.pid)])

PID TTY STAT TIME MAJFL TRS DRS RSS %MEM 命令16150 pts/16 Z+ 0:00 0 0 0 0 0.0 [a.out]

我的示例 C++ 程序之一:

One of my sample C++ program:

`int a[1000000];
int main()
{
    return 0;
}`

有时返回 pmem(rss=4096, vms=4313088) 有时返回 pmem(rss=0,vms=0)

returns pmem(rss=4096, vms=4313088) sometimes and pmem(rss=0,vms=0) sometimes

推荐答案

表示子进程是一个 僵尸进程(它已死,但父进程尚未读取其状态(p.poll()p.等待())).对于这样的进程,psutilps 似乎都显示 RSS 为零.

<defunct> 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.

结果取决于子进程是否会在调用 p.memory_info() 之前退出.这是一场比赛.如果您在 C++ 程序的退出处添加延迟,则 p.memory_info() 可能会在子进程退出之前被调用,并且您应该得到非零结果.

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?

您可能需要操作系统支持才能保存子进程退出后的内存使用信息.或者,您可以使用内存分析器(例如 valgrind)运行该程序并读取其结果.收集结果:

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 

要查看结果,您可以使用 ms_print:

To see the results, you could use ms_print:

$ ms_print massif.out.* | less

或 GUI Massif-Visualizer

@mdadm 建议一个更简单的解决方案:time 命令:

@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 时间使用 wait3() 来填充可用的资源使用信息.可以在 Python 中调用:

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)

我将 psutil.Process.memory_info (rss) 返回的最大值与 os.wait4 返回的 ru_maxrss 值进行了比较,并且valgrind --tool=massif 报告的最大总内存:它们是相似的.

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.

另见:

这篇关于如何在 Python 中使用 psutil 获取程序的最大内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆