python设置程序最大内存_如何在Python中使用psutil获得程序的最大内存使用率

博客探讨了在Python中使用psutil库获取程序内存使用情况时遇到的问题,包括初次运行时输出为0的情况以及不同环境下的内存使用差异。作者提到了进程可能成为僵尸状态导致的零内存输出,并提出了通过增加延迟解决竞态条件的方案。此外,还比较了psutil、os.wait4和valgrind等工具在监测内存使用上的效果。
摘要由CSDN通过智能技术生成

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)

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 is the name of the executable.

The output of command print subprocess.check_output(['ps', 'v', '-p', str(p.pid)])

PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND

16150 pts/16 Z+ 0:00 0 0 0 0 0.0 [a.out]

One of my sample C++ program:

`int a[1000000];

int main()

{

return 0;

}`

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

解决方案

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

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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值