我想知道如何在所有情况下以MB为单位获得进程的驻留内存使用量。有时我以MB为单位,但其他时候以GB为单位。进程内存使用量输出
我确实在寻找解决方案,但其中大多数都没有工作。可以工作到top -b -n 1 | grep %i | awk '{print$9}'
的东西会很棒。
我想知道如何在所有情况下以MB为单位获得进程的驻留内存使用量。有时我以MB为单位,但其他时候以GB为单位。进程内存使用量输出
我确实在寻找解决方案,但其中大多数都没有工作。可以工作到top -b -n 1 | grep %i | awk '{print$9}'
的东西会很棒。
如果您想避免使用子进程,您可以查看psutil库。
举个例子,如果你只是寻找信息中的一个进程:
import psutil
pid = 14551
def rss_MB(pid):
proc = psutil.Process(pid)
mem_bytes = proc.get_memory_info().rss
return(float(mem_bytes)/1048576)
mem_MB = rss_MB(pid)
print "pid: %d has an rss of %f MB" % (pid, mem_MB)
一个例子:
import subprocess
lines = subprocess.check_output(['ps', '-xacuww']).splitlines()
del lines[0] # skip the header
for p in lines:
items = p.split()
print "Process:", items[-1], " memory:", items[5], "kB"
这是写在FreeBSD,其中RSS大小以KB为报道。 Linux上ps
的选项可能会有所不同,请查看手册页。
请发表您的代码。 –
那么现在没有代码,我想要做什么,但我使用top -b -n 1 | grep%i | awk'{print $ 9}'来查找进程的CPU百分比。 – icebox3d
那么为什么这被标记为python问题呢? –