Skip to content

Commit 2b0860b

Browse files
updating rendered version
1 parent be6a8be commit 2b0860b

File tree

30 files changed

+2010
-589
lines changed

30 files changed

+2010
-589
lines changed

.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: cbd7e031f47c4eeff9f0ceaf8bd09f7a
3+
config: 991000b9a46c5b7dfd2ded2cef37faea
44
tags: 645f666f9bcd5a90fca523b33c5a78b7
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
mem_check.py
3+
4+
functions for getting memoroy use of the current python process
5+
6+
Windows and *nix versions
7+
8+
USAGE:
9+
10+
amount = get_mem_use(units='MB') # options are KB, MB, GB
11+
12+
"""
13+
14+
import sys
15+
16+
div = {'GB': 1024*1024*1024,
17+
'MB': 1024*1024,
18+
'KB': 1024,
19+
}
20+
21+
if sys.platform.startswith('win'):
22+
23+
"""
24+
25+
Functions for getting memory usage of Windows processes.
26+
27+
from:
28+
29+
http://code.activestate.com/recipes/578513-get-memory-usage-of-windows-processes-using-getpro/
30+
31+
get_mem_use(units='MB') is the one to get memory use for the current process.
32+
33+
34+
"""
35+
import ctypes
36+
from ctypes import wintypes
37+
38+
GetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
39+
GetCurrentProcess.argtypes = []
40+
GetCurrentProcess.restype = wintypes.HANDLE
41+
42+
SIZE_T = ctypes.c_size_t
43+
44+
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
45+
_fields_ = [
46+
('cb', wintypes.DWORD),
47+
('PageFaultCount', wintypes.DWORD),
48+
('PeakWorkingSetSize', SIZE_T),
49+
('WorkingSetSize', SIZE_T),
50+
('QuotaPeakPagedPoolUsage', SIZE_T),
51+
('QuotaPagedPoolUsage', SIZE_T),
52+
('QuotaPeakNonPagedPoolUsage', SIZE_T),
53+
('QuotaNonPagedPoolUsage', SIZE_T),
54+
('PagefileUsage', SIZE_T),
55+
('PeakPagefileUsage', SIZE_T),
56+
('PrivateUsage', SIZE_T),
57+
]
58+
59+
GetProcessMemoryInfo = ctypes.windll.psapi.GetProcessMemoryInfo
60+
GetProcessMemoryInfo.argtypes = [
61+
wintypes.HANDLE,
62+
ctypes.POINTER(PROCESS_MEMORY_COUNTERS_EX),
63+
wintypes.DWORD,
64+
]
65+
GetProcessMemoryInfo.restype = wintypes.BOOL
66+
67+
def get_current_process():
68+
"""Return handle to current process."""
69+
return GetCurrentProcess()
70+
71+
def get_memory_info(process=None):
72+
"""Return Win32 process memory counters structure as a dict."""
73+
if process is None:
74+
process = get_current_process()
75+
counters = PROCESS_MEMORY_COUNTERS_EX()
76+
ret = GetProcessMemoryInfo(process, ctypes.byref(counters),
77+
ctypes.sizeof(counters))
78+
if not ret:
79+
raise ctypes.WinError()
80+
info = dict((name, getattr(counters, name))
81+
for name, _ in counters._fields_)
82+
return info
83+
84+
def get_mem_use(units='MB'):
85+
"""
86+
returns the total memory use of the current python process
87+
88+
:param units='MB': the units you want the reslut in. Options are:
89+
'GB', 'MB', 'KB'
90+
"""
91+
info = get_memory_info()
92+
return info['PrivateUsage'] / float(div[units])
93+
94+
95+
else: # for posix systems only tested on OS-X for now
96+
def get_mem_use(units='MB'):
97+
"""
98+
returns the total memory use of the current python process
99+
100+
:param units='MB': the units you want the reslut in. Options are:
101+
'GB', 'MB', 'KB'
102+
"""
103+
import resource
104+
#useage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
105+
div = {'GB': 1024*1024*1024,
106+
'MB': 1024*1024,
107+
'KB': 1024,
108+
}
109+
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / float(div[units])

0 commit comments

Comments
 (0)