我的主要目标是知道我的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框架。
我该如何实现?
这是一个基于(os,psutil)模块的简单Python方式。感谢(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线程会消耗这么多的内存?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。