getting memory information under NT

dan_knierim at my-deja.com dan_knierim at my-deja.com
Mon Jun 28 16:02:49 EDT 1999


In article <375E88A2.68C1F118 at opticominc.com>,
  Eric Renouf <erenouf at opticominc.com> wrote:
> 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 at 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).

Good luck,
Dan K.

--------------------------------------------------------------
Corporation: n. An ingenious device for obtaining individual profit
without individual responsibility. -- Ambrose Bierce


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list