How to get memory usage on Windows?

Wolfgang Strobl ws at mystrobl.de
Sun Sep 22 14:27:43 EDT 2002


"GerritM" <gmuller at worldonline.nl>:

>Searching in books , documentation (a.o. win32api) and net did not yet help
>I found this article on the net:
>http://www.developeriq.com/Magazinestories/02jul29memory.php3
>but how to call this function from Python?

I guess thanks to Mark Hammond you already have everything you need.

Have a look at  \Python22\Lib\site-packages\win32\Lib\win32pdhutil.py,
uncomment the test function, run it. 

I snarfed the  following snippet straight from win32pdhutil.py:

import win32pdh

def GetPerformanceAttributes(object, counter, \
 instance = None, inum=-1, 
 format = win32pdh.PDH_FMT_LONG, machine=None):
    path = win32pdh.MakeCounterPath( (machine,object,\
    instance, None, inum,counter) )
    hq = win32pdh.OpenQuery()
    try:
        hc = win32pdh.AddCounter(hq, path)
        try:
            win32pdh.CollectQueryData(hq)
            type, val = win32pdh.GetFormattedCounterValue(hc, format)
            return val
        finally:
            win32pdh.RemoveCounter(hc)
    finally:
        win32pdh.CloseQuery(hq)

counter=r'\Memory\Committed Bytes'

machine, object, instance, parentInstance, index, counterName = \
		win32pdh.ParseCounterPath(counter)
result = GetPerformanceAttributes(object, counterName, instance, \
index, win32pdh.PDH_FMT_DOUBLE, machine)

print result

>>> 264937472.0


-- 
Wir danken für die Beachtung aller Sicherheitsbestimmungen



More information about the Python-list mailing list