Available System Memory

Thomas Heller theller at python.net
Thu Nov 6 21:13:20 CET 2003


"rtheiss" <rtheiss at yahoo.com> writes:

> Hi Folks,
>
> This is hopefully an easy question. I would like to be able to check 
> available system memory on a WinXP machine from within python.  I've 
> checked the docs in the win32 modules, read the Python Cookbook, and 
> tried the Python Essential Reference. 
>
> No luck. 
>
> Any ideas?
>
> Many Thanks.
>
> Bob
>
> P.S.  using v2.2
>  
Here is a simple script which calls the GlobalMemoryStatus function:

-----
from ctypes import *
from ctypes.wintypes import DWORD

SIZE_T = c_ulong

class _MEMORYSTATUS(Structure):
    _fields_ = [("dwLength", DWORD),
                ("dwMemoryLength", DWORD),
                ("dwTotalPhys", SIZE_T),
                ("dwAvailPhys", SIZE_T),
                ("dwTotalPageFile", SIZE_T),
                ("dwAvailPageFile", SIZE_T),
                ("dwTotalVirtual", SIZE_T),
                ("dwAvailVirtualPhys", SIZE_T)]
    def show(self):
        for field_name, field_type in self._fields_:
            print field_name, getattr(self, field_name)

memstatus = _MEMORYSTATUS()
windll.kernel32.GlobalMemoryStatus(byref(memstatus))
memstatus.show()
-----

On my machine, it prints this:

dwLength 32
dwMemoryLength 63
dwTotalPhys 535609344
dwAvailPhys 198139904
dwTotalPageFile 907055104
dwAvailPageFile 642375680
dwTotalVirtual 2147352576
dwAvailVirtualPhys 2117771264

See the MSDN docs for GlobalMemoryStatus to learn what the fields mean,
and <http://starship.python.net/crew/theller/ctypes> for the ctypes
module.

Thomas




More information about the Python-list mailing list