获取 Python 应用程序的内存使用情况

5
我的主要目标是了解我的Python应用程序在执行期间占用了多少内存。
我正在使用Windows-32和Windows-64上的Python 2.7.5。
我在此找到了一种获取有关我的进程信息的方法:http://code.activestate.com/recipes/578513-get-memory-usage-of-windows-processes-using-getpro/ 以下是方便起见放置代码:
"""Functions for getting memory usage of Windows processes."""

__all__ = ['get_current_process', 'get_memory_info', 'get_memory_usage']

import ctypes
from ctypes import wintypes

GetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
GetCurrentProcess.argtypes = []
GetCurrentProcess.restype = wintypes.HANDLE

SIZE_T = ctypes.c_size_t

class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
    _fields_ = [
        ('cb', wintypes.DWORD),
        ('PageFaultCount', wintypes.DWORD),
        ('PeakWorkingSetSize', SIZE_T),
        ('WorkingSetSize', SIZE_T),
        ('QuotaPeakPagedPoolUsage', SIZE_T),
        ('QuotaPagedPoolUsage', SIZE_T),
        ('QuotaPeakNonPagedPoolUsage', SIZE_T),
        ('QuotaNonPagedPoolUsage', SIZE_T),
        ('PagefileUsage', SIZE_T),
        ('PeakPagefileUsage', SIZE_T),
        ('PrivateUsage', SIZE_T),
    ]

GetProcessMemoryInfo = ctypes.windll.psapi.GetProcessMemoryInfo
GetProcessMemoryInfo.argtypes = [
    wintypes.HANDLE,
    ctypes.POINTER(PROCESS_MEMORY_COUNTERS_EX),
    wintypes.DWORD,
]
GetProcessMemoryInfo.restype = wintypes.BOOL

def get_current_process():
    """Return handle to current process."""
    return GetCurrentProcess()

def get_memory_info(process=None):
    """Return Win32 process memory counters structure as a dict."""
    if process is None:
        process = get_current_process()
    counters = PROCESS_MEMORY_COUNTERS_EX()
    ret = GetProcessMemoryInfo(process, ctypes.byref(counters),
                               ctypes.sizeof(counters))
    if not ret:
        raise ctypes.WinError()
    info = dict((name, getattr(counters, name))
                for name, _ in counters._fields_)
    return info

def get_memory_usage(process=None):
    """Return this process's memory usage in bytes."""
    info = get_memory_info(process=process)
    return info['PrivateUsage']

if __name__ == '__main__':
    import pprint
    pprint.pprint(get_memory_info())

这是结果:

{'PageFaultCount': 1942L,
 'PagefileUsage': 4624384L,
 'PeakPagefileUsage': 4624384L,
 'PeakWorkingSetSize': 7544832L,
 'PrivateUsage': 4624384L,
 'QuotaNonPagedPoolUsage': 8520L,
 'QuotaPagedPoolUsage': 117848L,
 'QuotaPeakNonPagedPoolUsage': 8776L,
 'QuotaPeakPagedPoolUsage': 117984L,
 'WorkingSetSize': 7544832L,
 'cb': 44L}

但这并不能满足我的需求。这些结果给了我整个Python进程的信息,而我需要的仅仅是运行在Python框架之上的特定应用程序。
我在互联网上看到了几个内存分析器,也在Stack Overflow上看到了一些,但它们对我来说太大了。我所需要的唯一信息就是我的应用程序自身消耗了多少内存-而不是考虑整个Python框架。
如何实现这一点?

1
Memory_profiler 可以逐行跟踪 Python 程序的内存使用情况。 - ali_m
我尝试了一下,它只在执行后才给出结果。但我的应用程序需要运行数天甚至数周,因此我不能使用它进行检查。 - Bush
此外,我的应用程序包含了tkinter的GUI和多个线程,因此似乎对我来说根本无法工作。 - Bush
1
好的,那就用psutil吧。获取你的父进程PID,然后使用psutil.Process(mypid).get_memory_info() - ali_m
2个回答

3
这是一种简单易懂的Pythonic方法,基于(os,psutil)模块。感谢(Dataman)和(RichieHindle)的答案。
import os
import psutil


## - Get Process Id of This Running Script -
proc_id = os.getpid()

print '\nProcess ID: ', proc_id


#--------------------------------------------------
## - Get More Info Using the Process Id

ObjInf = psutil.Process(proc_id)

print '\nProcess %s Info:' % proc_id, ObjInf

#--------------------------------------------------
## - Proccess Name of this program 

name = ObjInf.name()

print '\nThis Program Process name:', name

#--------------------------------------------------
## - Print CPU Percentage

CpuPerc = ObjInf.cpu_percent()

print '\nCpu Percentage:', CpuPerc


#---------------------------------------------------
## - Print Memory Usage

memory_inf = ObjInf.memory_full_info()

print '\nMemory Info:', memory_inf, '\n'



## Print available commands you can do with the psutil obj

for c in dir(ObjInf):
    print c 

如果你的脚本是用 Python 编写的,那么你的脚本本身就是 Python,所以没有 Python 就无法运行脚本。因此,你还需要考虑 Python 的内存使用情况。如果你想查看 Python 自身消耗了多少内存,只需运行一个空的 Python 脚本,然后从中减去即可,这样你的脚本将成为主要的资源消耗者,而它恰好是用 Python 编写的。
现在,如果你想检查线程的内存使用情况,那么这个问题可能会有所帮助 -> 为什么 Python 线程消耗如此之多的内存?

1
这是我使用的脚本,用于查找另一个脚本执行过程中使用的最大资源量。我正在使用 psutil 来实现这一目的。您可以调整脚本以使其适合您的需求。
import psutil, sys
import numpy as np
from time import sleep
pid = int(sys.argv[1])
delay = int(sys.argv[2])
p = psutil.Process(pid)
max_resources_used = -1
while p.is_running():
    ## p.memory_info()[0] returns 'rss' in Unix
    r = int(p.memory_info()[0] / 1048576.0)  ## resources used in Mega Bytes
    max_resources_used = r if r > max_resources_used else max_resources_used
    sleep(delay)
print("Maximum resources used: %s MB." %np.max(max_resources_used))

使用方法:

python script.py pid delay_in_seconds

例如:

python script.py  55356  2 

说明:

您需要找出进程 ID 并将其作为参数传递给脚本,再加上检查资源使用情况的时间间隔(即脚本每隔多少秒检查一次已使用的资源量)。该脚本会跟踪内存使用情况,直到该进程停止。最后,它会返回以 MB 为单位的最大内存使用量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接