Python 如果你通过了-v,你就可以得到这些信息。请参阅下面的最大驻留集大小,它是程序执行期间使用的最大(峰值)真实(非虚拟)内存: $ /usr/bin/time -v ls / Command being timed: "ls /" User

Python 如果你通过了-v,你就可以得到这些信息。请参阅下面的最大驻留集大小,它是程序执行期间使用的最大(峰值)真实(非虚拟)内存: $ /usr/bin/time -v ls / Command being timed: "ls /" User ,python,memory-management,Python,Memory Management,如果你通过了-v,你就可以得到这些信息。请参阅下面的最大驻留集大小,它是程序执行期间使用的最大(峰值)真实(非虚拟)内存: $ /usr/bin/time -v ls / Command being timed: "ls /" User time (seconds): 0.00 System time (seconds): 0.01 Percent of CPU this job got: 250% Elapsed (wall clock) time (

如果你通过了-v,你就可以得到这些信息。请参阅下面的
最大驻留集大小
,它是程序执行期间使用的最大(峰值)真实(非虚拟)内存:

$ /usr/bin/time -v ls /

    Command being timed: "ls /"
    User time (seconds): 0.00
    System time (seconds): 0.01
    Percent of CPU this job got: 250%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 0
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 315
    Voluntary context switches: 2
    Involuntary context switches: 0
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0


Windows代码对我不起作用。此更改是:
return int(result[0]。WorkingSet)
即使在John Fouhy的注释修改之后,此Windows代码在Windows 7 x64上也不适用于我。我有以下错误:在self中为obj返回[wmi\u对象(obj,instance\u of,fields)。\u raw\u query(wql)]文件“C:\Python27\lib\site packages\win32com\client\util.py”,第84行,在下一个返回中,pywintypes.com错误:(-2147217385,'OLE错误0x80041017',无,无),如果有人可以帮助我?我在X32上赢得了8 x64但是python注意:在检查wmi模块的(最新)源代码之后,我按照John Fouhy的建议更新了windows示例。另请参见,.@jedwards:WorkingSet在>90%的情况下是一个非常糟糕的数字。这只是内存的一部分,目前在Ramook中,就可以了。我不确定是否有一个合并问题的流程。这篇重复的帖子部分是为了向人们展示这两个问题都有一个标准的图书馆解决方案。。。部分是为了代表;)我应该删除这个答案吗?Mac OS肯定以字节为单位返回RSS,Linux以KB为单位返回RSS。单位不是KB。它依赖于平台,因此您必须使用resource.getpagesize()来查找。给定的Python文档()实际上对此非常清楚。在我的盒子里是4096。@BenLin那些Python文档显然是错的,或者Mac版本上有一个bug。getrusage使用的单位和getpagesize返回的值完全不同。该问题询问了当前使用情况。请注意,这是最大使用量。(这仍然是一个有用的答案,只是警告那些错误复制粘贴它的人。)应该注意的是,“sh”不是stdlib模块。但是,它可以通过pip安装。
psutil
是跨平台的,可以返回与
ps
命令行工具相同的值:为什么这个数字与process explorer中的数字不匹配?psutil中的数字似乎总是大于10%。请注意,psutil不在标准库中。对于最新版本的
psutil
psutil.Process()
相当于
psutil.Process(os.getpid())
。这是你需要记住的一件事,你为什么要使用rss?在psutil文档中:rss |常驻集大小,VM |总程序大小。所以我认为最后一行应该是
print(process.memory\u info().vms)
这可以通过解释它的功能和工作原理来改进。根据返回的大量数字(8位)以及我没有做很多事情,我猜这一定是字节?因此,对于一个相当空闲的交互实例来说,它大约是28.5MB。(哇……我甚至没有意识到上面的评论是我4年前写的……这很奇怪。)只是对代码进行了优化,以避免多管
ps aux | awk'/python/{sum+=6};结束{print sum/1024“MB”}
注意,如果您只是尝试使用
time
而不是
/usr/bin/time
,则此操作可能会失败。请参阅:这需要lib psutil
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
>>> resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
2656  # peak memory usage (kilobytes on Linux, bytes on OS X)
float(sh.awk(sh.ps('u','-p',os.getpid()),'{sum=sum+$6}; END {print sum/1024}'))
import os, psutil
process = psutil.Process(os.getpid())
print(process.memory_info().rss)  # in bytes 
print(process.memory_info()[0])
import os, psutil; print(psutil.Process(os.getpid()).memory_info().rss / 1024 ** 2)
import os, win32api, win32con, win32process
han = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION|win32con.PROCESS_VM_READ, 0, os.getpid())
process_memory = int(win32process.GetProcessMemoryInfo(han)['WorkingSetSize'])
# 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
import time
import os
import psutil


def elapsed_since(start):
    return time.strftime("%H:%M:%S", time.gmtime(time.time() - start))


def get_process_memory():
    process = psutil.Process(os.getpid())
    return process.memory_info().rss


def track(func):
    def wrapper(*args, **kwargs):
        mem_before = get_process_memory()
        start = time.time()
        result = func(*args, **kwargs)
        elapsed_time = elapsed_since(start)
        mem_after = get_process_memory()
        print("{}: memory before: {:,}, after: {:,}, consumed: {:,}; exec time: {}".format(
            func.__name__,
            mem_before, mem_after, mem_after - mem_before,
            elapsed_time))
        return result
    return wrapper
from utils import track

@track
def list_create(n):
    print("inside list create")
    return [1] * n
inside list create
list_create: memory before: 45,928,448, after: 46,211,072, consumed: 282,624; exec time: 00:00:00
import os
import psutil
process = psutil.Process(os.getpid())
print(process.memory_percent())
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
$ /usr/bin/time -v ls /

    Command being timed: "ls /"
    User time (seconds): 0.00
    System time (seconds): 0.01
    Percent of CPU this job got: 250%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 0
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 315
    Voluntary context switches: 2
    Involuntary context switches: 0
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0