faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 6 people (50%) answered Yes
Recently 2 of 5 people (40%) answered Yes

Entry

Getting memory information under NT

Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 171, Dan Knierim


"""
Packages: operating_systems.windows
"""
"""
> Does anyone know of an easy way to get the the total physical memory
> and/or available memory on an NT system using Python 1.5.2?  Is there
a
> module that has functions for things like this already?
>
> Thanks,
> Eric Renouf
>
I earlier posted an incorrect response to your inquiry, sorry.
A better answer is: look at Sam Rushing's windll and structob modules,
part of the DynWin software available from the python.org download site,
or from http://nightmare.com/software.html.  These use Mr. Rushing's
calldll and npstruct utilities, which are also available from those
sites.
The downloads include some very helpful demo code, but the demos I
got seem not quite in sync with the release of DynWin that I have.  In
particular the module dlldemo.py has an example of how to call the
Windows function 'GlobalMemoryStatus' as follows (edited for brevity):
"""
# -*- Mode: Python; tab-width: 4 -*-
# Author: Sam Rushing <rushing@nightmare.com>
import windll, structob
kernel32 = windll.module ('kernel32')
memstat = structob.Oracle ('memory status structure', 'Nllllllll',
                           ('Length', 'MemoryLoad', 'TotalPhys',
                            'AvailPhys', 'TotalPageFile',
                            'AvailPageFile', 'TotalVirtual',
                            'AvailVirtual') )
def global_memory_status():
	buffer = windll.membuf (memstat.size)
	result = kernel32.GlobalMemoryStatus (buffer.address())
	dict, size = memstat.unpack (buffer.read())
	memstat.describe (dict)
#----------- end of code fragment
"""
I haven't checked the following with the author or any Python wizes, but
I'll explain what I think based on my experiments and reading bits of
the code.
The second parameter in the call to structob.Oracle, 'Nllllllll', is a
format code, and the first character 'N' is supposed to indicate whether
the data structure uses big-endian or little-endian byte order.  However
this value must be for a different version of npstruct, because the
release I have of npstruct.py (which interprets the format code) expects
either an 'L' (for little-endian) or a 'B' (for big-endian) as the first
character.  After I replaced the 'N' with 'L', the assignment to memstat
looks like
memstat = structob.Oracle ( 'memory status structure', 'Lllllllll',
etc.
The demo now runs fine.
The Win32 extensions from Mark Hammond (also available from
www.python.org in the win32all download) provide an interface to the
Performance Monitor counters, in modules win32pdh*.py.  This gives
access to several detailed measures related to memory usage (the same
ones available through the NT Performance Monitor admin tool).
"""