from datetime import datetime
import psutil
import time
if __name__ == "__main__":
while True:
# 获取所有正在运行的进程PID
pid_list = psutil.pids()
chrome_sum = 0
chrome_mem_percnet = 0
chrome_cpu_percnet = 0
chrome_cpu_times = 0
chrome_mem_rss = 0
chrome_mem_vms = 0
chrome_mem_uss = 0
for pid in pid_list:
try:
pid_process = psutil.Process(pid)
except Exception as e:
continue
pid_name = pid_process.name()
if "chrome.exe" == pid_name:
chrome_sum = chrome_sum + 1
chrome_mem_percnet = chrome_mem_percnet + pid_process.memory_percent()
chrome_cpu_percnet = chrome_cpu_percnet + pid_process.cpu_percent()
chrome_mem_rss = chrome_mem_rss + pid_process.memory_info().rss
chrome_mem_vms = chrome_mem_vms + pid_process.memory_info().vms
chrome_mem_uss = chrome_mem_uss + pid_process.memory_full_info().uss #USS(唯一集大小)是进程专有的内存
chrome_cpu_times = chrome_cpu_times + pid_process.cpu_times().user
print(str(chrome_mem_uss/1024/1024))
with open("mem",'a') as mem_file:
now_time = datetime.fromtimestamp(round(time.time(), 0))
mem_file.write(str(now_time) + "," + str(round(chrome_mem_uss/1024/1024,2)) + "\n")
time.sleep(0.5)
|