1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import os, re import time import string
def countProcessMemoey(processName): pattern = re.compile(r'([^\s]+)\s+(\d+)\s.*\s([^\s]+\sK)') cmd = 'tasklist /fi "imagename eq ' + processName + '"' + ' | findstr.exe ' + processName result = os.popen(cmd).read() resultList = result.split("\n")
total = 0 for srcLine in resultList: srcLine = "".join(srcLine.split('\n')) if len(srcLine) == 0: break m = pattern.search(srcLine) if m == None: continue
if str(os.getpid()) == m.group(2): continue ori_mem = m.group(3).replace(',','') ori_mem = ori_mem.replace(' K','') ori_mem = ori_mem.replace(r'\sK','') memEach = int(ori_mem) * 1.0 / 1024 total += memEach print('ProcessName:'+ m.group(1) + '\tPID:' + m.group(2) + '\tmemory size:%.2f'% memEach, 'M') print('total: ', total, 'M', sep='') print("*" * 58)
if __name__ == '__main__': ProcessName = 'python.exe' while True: countProcessMemoey(ProcessName) time.sleep(5)
|