I'm writing a Python script that runs another program as a subprocess, like
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = p.communicate()
stdout = str(stdout, "utf-8")
stderr = str(stderr, "utf-8")
I need to check how much time and memory the program used. Time is easy. Memory is tricky.
There was another question that asked the same thing except without mentioning platform, and all the answers were Unix-specific, from which I take it that there is no cross-platform solution. Okay, I'm on Windows, so now I'm looking for a (non-WSL) Windows-specific solution.
The ideal would be if I could set a hard limit on the amount of memory the subprocess is allowed to use (so that it would crash if it tries to exceed that), but I think at this stage there is no practical way to do that on Windows. I'll settle for a way to just report on how much it ended up using.
How can Python on Windows, check how much memory a subprocess used?
CodePudding user response:
After a quick search I found this, is this what you're looking for?
import os
import psutil
process = psutil.Process(os.getpid())
print(process.memory_info().rss)