How to get Windows physical RAM using python?

Seo Sanghyeon unendliche at hanmail.net
Thu Jul 31 03:18:05 EDT 2003


Mark wrote:
> OK, How to check the amount of Windows physical RAM using python?

Martin v. Lowis wrote:
> You should call the GlobalMemoryStatus(Ex) function. To my knowledge,
> there is no Python wrapper for it, yet, so you would need to write one.

So let's write one in Python in a minute!

---- winmem.py
from ctypes import *
from ctypes.wintypes import *

class MEMORYSTATUS(Structure):
    _fields_ = [
        ('dwLength', DWORD),
        ('dwMemoryLoad', DWORD),
        ('dwTotalPhys', DWORD),
        ('dwAvailPhys', DWORD),
        ('dwTotalPageFile', DWORD),
        ('dwAvailPageFile', DWORD),
        ('dwTotalVirtual', DWORD),
        ('dwAvailVirtual', DWORD),
    ]

def winmem():
    x = MEMORYSTATUS()
    windll.kernel32.GlobalMemoryStatus(byref(x))
    return x

---- in your code
>>> from winmem import winmem
>>> m = winmem()
>>> print '%d MB physical RAM left.' % (m.dwAvailPhys/1024**2)
90 MB physical RAM left.
>>> 


Hail to ctypes!

If you have never heard of ctypes, visit
http://starship.python.net/crew/theller/ctypes/ and try it. You
will love it.

Seo Sanghyeon




More information about the Python-list mailing list