How to get Windows physical RAM using python?

Richie Hindle richie at entrian.com
Thu Jul 31 06:26:07 EDT 2003


[Mark]
> How to check the amount of Windows physical RAM using python?

[Martin]
> 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.

Easy with ctypes:

------------------------------------------------------------------------------

from ctypes import *

kernel32 = windll.kernel32

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

def getTotalPhysicalBytes():
    memoryStatus = MEMORYSTATUS()
    memoryStatus.dwLength = sizeof(MEMORYSTATUS)
    kernel32.GlobalMemoryStatus(byref(memoryStatus))
    return memoryStatus.dwTotalPhys

if __name__ == '__main__':
    bytes = getTotalPhysicalBytes()
    print "Total physical RAM: %d bytes (%dMB)" % (bytes, bytes / 1024 / 1024)

------------------------------------------------------------------------------

-- 
Richie Hindle
richie at entrian.com






More information about the Python-list mailing list