getting cross platform system information

David Bolen db3l at fitlinxx.com
Mon Jan 6 17:08:46 CET 2003


"Simon Bunker" <simon at rendermania.com> writes:

> by RAM usage I mean total free memory - eg what free gives you under Linux.
> Unix/Linux isn't really the problem, but Windows is as I don't know any
> win32 programming.

As others have pointed out, there are a lot of variables in terms of
just what you want to report in terms of memory usage.  But if you
want values that are basically what the Windows Task Manager (at least
under NT/2K/XP - but the API is documented for the 9x series too)
reports, you can use the GlobalMemoryStatus Win32 API function (see
MSDN for the API definition).  I believe it still remains unwrapped by
the win32all package, but here's some example code accessing it with
the available calldll module.  I'm sure you could also use you can
access it with the available ctypes module (and perhaps more simply),
but that didn't exist when I originally wrote this code.

          - - - - - - - - - - - - - - - - - - - - - - - - -
    import calldll, struct

    result   = {}
    kernel32 = calldll.load_library('kernel32')
    memstat  = calldll.get_proc_address(kernel32,'GlobalMemoryStatus')
    if memstat:
        buf = calldll.membuf(32)
        buf.write(struct.pack('1L',32))
        calldll.call_foreign_function(memstat,'l','',(buf.address(),))
        rtuple = struct.unpack('8L',buf.read())
        result['MemoryLoad']    = rtuple[1]   # [0] is len
        result['TotalPhys']     = rtuple[2]
        result['AvailPhys']     = rtuple[3]
        result['TotalPageFile'] = rtuple[4]
        result['AvailPageFile'] = rtuple[5]
        result['TotalVirtual']  = rtuple[6]
        result['AvailVirtual']  = rtuple[7]
    return result
          - - - - - - - - - - - - - - - - - - - - - - - - -

The memory load is in percentage - the other values are all bytes.
This API may not be accurate with more than 2GB of memory in a machine
(it rounds some values down to 2GB) and it will overflow at 4GB -
there's a GlobalMemoryStatusEx call that uses 64-bit return values if
you are worried about such cases, but that's unsupported on the Win 9x
series.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/




More information about the Python-list mailing list