Python resource.getrusage表示没有分配新的内存

时间:2017-02-27 18:51:05

标签: python

在下文中,我尝试使用以下方法测量当前进程的内存使用情况 resource.getrusage。

我分配一个大型数组(并且可以通过psutil看到系统内存增加),但getrusage在分配数组之前和之后报告相同的rss值。那是什么?

import psutil
import os
import resource
import numpy as np


def total_memory():
    v = psutil.virtual_memory()
    s = psutil.swap_memory()
    return v.used + s.used

result = resource.getrusage(resource.RUSAGE_SELF)
print "resource START ", result.ru_maxrss, result.ru_ixrss, result.ru_idrss, result.ru_isrss
init = total_memory()
print "psutil   START ", init

data = np.random.rand(10000,10000)

result = resource.getrusage(resource.RUSAGE_SELF)
print "resource   END ", result.ru_maxrss, result.ru_ixrss, result.ru_idrss, result.ru_isrss
print "psutil     END+", total_memory()-init

报告:

resource START  1586580 0 0 0
psutil   START  5074391040

resource   END  1586580 0 0 0  # same
psutil     END+ 793374720  # total memory increase by this much

Python 2.7.6。 Ubuntu 14

0 个答案:

没有答案