通过ctypes获得python windows process的内存使用情况
2013-08-05 20:23
537 查看
通过ctypes 类库中的win32方法GetProcessMemoryInfo()获得当前进程的内存使用情况。该函数可以在32或者64位,python2.6+及python3.x之上都能有用。
"""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())
相关文章推荐
- python windows下通过SSH获取linux系统cpu、内存、网络使用情况
- windows下使用adb工具查看android程序cpu和内存消耗情况
- Windows下用C语言获取进程cpu使用率,内存使用,IO情况
- [zz]通过win32扩展接口使用 Python获得系统进程列表的方法
- linux下通过go语言获得系统进程cpu使用情况的方法
- 转载:python获取linux系统内存、cpu、网络使用情况
- Loadrunner 监控Windows资源(CPU、内存)使用情况
- PHP通过内置函数memory_get_usage()获取内存使用情况
- python 使用socket通过用户自定义输入获得远程设备IP
- Linux下获得CPU利用率和内存使用情况(C实现)
- C++编程中,利用WINDOWS API获得系统状态信息[CPU占用率,硬盘使用情况,内存使用情况]
- C++编程中,利用WINDOWS API获得系统状态信息[CPU占用率,硬盘使用情况,内存使用情况]
- 通过jmc监控spark应用的内存使用情况
- [Z]监控Java应用程序Windows内存使用情况
- Windows下用C语言获取进程cpu使用率,内存使用,IO情况
- windows通过VisualVM远程监控linux下的tomcat使用情况
- Python以邮件的方式发送由各个区服Redis数据库内存使用情况组成的Zabbix Screen图片
- Linux下获得CPU利用率和内存使用情况(C实现)
- python在linux系统下获取系统内存使用情况的方法
- 获取对应进程的内存使用情况(GetProcessMemoryInfo)