How to get the memory working set of a 64bit proce

2019-07-25 16:20发布

问题:

for an automated test framework I need to monitor the working set of a 64 bit process that I started. Somehow I can't find a solution that allows to monitor more than 4GB data. I tried WMI and psutil, but both are stuck in 32bit borders. I do something like this:

import wmi
import psutil
import subprocess 

def measure_memory( process ):
    mem = psutil.Process( process.pid ).get_memory_info().rss
    return "%.2f M" % (float(mem)/1024./1024.)

def measure_memory_wmi( process ):
    w = wmi.WMI('.')
    result = w.query("SELECT WorkingSet FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess="+str(process.pid))
    subset = result[0]
    return "%.2f M" % (float(subset.WorkingSet)/1024./1024.)

process = subprocess.Popen( [path_to_program, '-option'] )
print measure_memory( process )
print measure_memory_wmi( process )

this delivers:

-0.00 M
4096.00 M

while the process explorer delivers

6.806.976 K

is there another way to get the real data?

Thanks!

回答1:

Ok, after some WMI tools / google accident I found another way to phrase a query that delivers correct numbers. This is the new code:

def measure_memory( process ):
    w = wmi.WMI('.')
    result = w.query("SELECT WorkingSetSize FROM Win32_Process WHERE ProcessID="+str(process.pid))
    print result
    subset = result[0]
    return "%.2f" % (float(subset.WorkingSetSize)/1024./1024.)

This seems to work now. Can anyone confirm that this should work for more than my case now?