是否有一种方法可以让Python程序确定它当前使用了多少内存?我看到过关于单个对象的内存使用情况的讨论,但我需要的是进程的总内存使用情况,这样我就可以确定何时需要开始丢弃缓存的数据。


当前回答

甚至比/proc/self/status更容易使用:/proc/self/statm.它只是一个由几个统计数据以空格分隔的列表。我无法判断这两个文件是否始终存在。

/proc/[pid]/statm Provides information about memory usage, measured in pages. The columns are: size (1) total program size (same as VmSize in /proc/[pid]/status) resident (2) resident set size (same as VmRSS in /proc/[pid]/status) shared (3) number of resident shared pages (i.e., backed by a file) (same as RssFile+RssShmem in /proc/[pid]/status) text (4) text (code) lib (5) library (unused since Linux 2.6; always 0) data (6) data + stack dt (7) dirty pages (unused since Linux 2.6; always 0)

这里有一个简单的例子:

from pathlib import Path
from resource import getpagesize

PAGESIZE = getpagesize()
PATH = Path('/proc/self/statm')


def get_resident_set_size() -> int:
    """Return the current resident set size in bytes."""
    # statm columns are: size resident shared text lib data dt
    statm = PATH.read_text()
    fields = statm.split()
    return int(fields[1]) * PAGESIZE


data = []
start_memory = get_resident_set_size()
for _ in range(10):
    data.append('X' * 100000)
    print(get_resident_set_size() - start_memory)

生成的列表如下所示:

0
0
368640
368640
368640
638976
638976
909312
909312
909312

你可以看到,在大约分配了3次10万字节后,它增加了大约30万字节。

其他回答

这里有一个有用的解决方案,适用于各种操作系统,包括Linux, Windows等:

import os, psutil
process = psutil.Process(os.getpid())
print(process.memory_info().rss)  # in bytes 

注:

如果还没有安装,PIP是否安装psutil 如果你想快速知道你的进程需要多少MiB,这是一个方便的单行程序: 导入os, psutil;打印(psutil.Process (os.getpid ()) .memory_info()。RSS / 1024 ** 2) 在Python 2.7和psutil 5.6.3中,它是process.memory_info()[0](后来在API中有了更改)。

使用sh和os进入python拜耳的答案。

float(sh.awk(sh.ps('u','-p',os.getpid()),'{sum=sum+$6}; END {print sum/1024}'))

答案是以兆字节为单位。

对于基于Unix的系统(Linux、Mac OS X、Solaris),可以使用标准库模块资源中的getrusage()函数。结果对象具有ru_maxrss属性,该属性给出了调用进程的内存使用峰值:

>>> resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
2656  # peak memory usage (kilobytes on Linux, bytes on OS X)

Python文档不记录单位。请参考您的特定系统的man getusage。2页检查该单位的值。在Ubuntu 18.04中,单位是千字节。在Mac OS X上,它是字节。

getrusage()函数也可以被赋予资源。获取子进程和(在某些系统上)资源的使用情况。RUSAGE_BOTH用于总(自我和子)进程使用情况。

如果你只关心Linux,你也可以阅读/proc/self/status或/proc/self/statm文件,就像这个问题和这个问题的其他答案中描述的那样。

在Windows上,你可以使用WMI(主页,cheeseshop):

def memory():
    import os
    from wmi import WMI
    w = WMI('.')
    result = w.query("SELECT WorkingSet FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess=%d" % os.getpid())
    return int(result[0].WorkingSet)

在Linux上(来自python烹饪书http://code.activestate.com/recipes/286222/:

import os
_proc_status = '/proc/%d/status' % os.getpid()

_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0, 'KB': 1024.0, 'MB': 1024.0*1024.0}

def _VmB(VmKey):
    '''Private.'''
    global _proc_status, _scale
     # get pseudo file  /proc/<pid>/status
    try:
        t = open(_proc_status)
        v = t.read()
        t.close()
    except:
        return 0.0  # non-Linux?
     # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
    i = v.index(VmKey)
    v = v[i:].split(None, 3)  # whitespace
    if len(v) < 3:
        return 0.0  # invalid format?
     # convert Vm value to bytes
    return float(v[1]) * _scale[v[2]]

def memory(since=0.0):
    '''Return memory usage in bytes.'''
    return _VmB('VmSize:') - since

def resident(since=0.0):
    '''Return resident memory usage in bytes.'''
    return _VmB('VmRSS:') - since

def stacksize(since=0.0):
    '''Return stack size in bytes.'''
    return _VmB('VmStk:') - since

我喜欢,谢谢@拜耳。现在我有了一个特定的进程计数工具。

# Megabyte.
$ ps aux | grep python | awk '{sum=sum+$6}; END {print sum/1024 " MB"}'
87.9492 MB

# Byte.
$ ps aux | grep python | awk '{sum=sum+$6}; END {print sum " KB"}'
90064 KB

附上我的流程清单。

$ ps aux  | grep python
root       943  0.0  0.1  53252  9524 ?        Ss   Aug19  52:01 /usr/bin/python /usr/local/bin/beaver -c /etc/beaver/beaver.conf -l /var/log/beaver.log -P /var/run/beaver.pid
root       950  0.6  0.4 299680 34220 ?        Sl   Aug19 568:52 /usr/bin/python /usr/local/bin/beaver -c /etc/beaver/beaver.conf -l /var/log/beaver.log -P /var/run/beaver.pid
root      3803  0.2  0.4 315692 36576 ?        S    12:43   0:54 /usr/bin/python /usr/local/bin/beaver -c /etc/beaver/beaver.conf -l /var/log/beaver.log -P /var/run/beaver.pid
jonny    23325  0.0  0.1  47460  9076 pts/0    S+   17:40   0:00 python
jonny    24651  0.0  0.0  13076   924 pts/4    S+   18:06   0:00 grep python

参考

内存- Linux:找出什么进程正在使用所有的RAM?—超级用户 Python进程使用的内存总数?-堆叠溢出 linux - ps aux输出含义-超级用户