1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 r"""memutils.py: Memory handling utilities.
19
20 """
21 __docformat__ = "restructuredtext en"
22
23 import os
24
26 """Try getting a value for the physical memory using os.sysconf().
27
28 Returns None if no value can be obtained - otherwise, returns a value in
29 bytes.
30
31 """
32 if getattr(os, 'sysconf', None) is None:
33 return None
34
35 try:
36 pagesize = os.sysconf('SC_PAGESIZE')
37 except ValueError:
38 try:
39 pagesize = os.sysconf('SC_PAGE_SIZE')
40 except ValueError:
41 return None
42
43 try:
44 pagecount = os.sysconf('SC_PHYS_PAGES')
45 except ValueError:
46 return None
47
48 return pagesize * pagecount
49
51 """Try getting a value for the physical memory using GlobalMemoryStatus.
52
53 This is a windows specific method. Returns None if no value can be
54 obtained (eg, not running on windows) - otherwise, returns a value in
55 bytes.
56
57 """
58 try:
59 import ctypes
60 import ctypes.wintypes as wintypes
61 except ValueError:
62 return None
63
64 class MEMORYSTATUS(wintypes.Structure):
65 _fields_ = [
66 ('dwLength', wintypes.DWORD),
67 ('dwMemoryLoad', wintypes.DWORD),
68 ('dwTotalPhys', wintypes.DWORD),
69 ('dwAvailPhys', wintypes.DWORD),
70 ('dwTotalPageFile', wintypes.DWORD),
71 ('dwAvailPageFile', wintypes.DWORD),
72 ('dwTotalVirtual', wintypes.DWORD),
73 ('dwAvailVirtual', wintypes.DWORD),
74 ]
75
76 m = MEMORYSTATUS()
77 wintypes.windll.kernel32.GlobalMemoryStatus(wintypes.byref(m))
78 return m.dwTotalPhys
79
81 """Get the amount of physical memory in the system, in bytes.
82
83 If this can't be obtained, returns None.
84
85 """
86 result = _get_physical_mem_sysconf()
87 if result is not None:
88 return result
89 return _get_physical_mem_win32()
90