Skip to content

Instantly share code, notes, and snippets.

@zed
Created March 29, 2014 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zed/9859060 to your computer and use it in GitHub Desktop.
Save zed/9859060 to your computer and use it in GitHub Desktop.
"""
Find maximum memory consumed by a process specified at the command-line.
Usage:
$ python memory-usage-wait3.py cmd arg1 arg2
"""
import os
import sys
import threading
from subprocess import Popen
import psutil # $ pip install psutil
def report_memory(pid):
p = psutil.Process(pid)
max_rss, max_vms = -1, -1
while True:
try:
rss, vms = p.memory_info()
except psutil.NoSuchProcess:
break
else:
if rss > max_rss:
max_rss = rss
if vms > max_vms:
max_vms = vms
print((max_rss//(1<<20), max_vms//(1<<20))) # MB
args = sys.argv[1:]
p = Popen(args)
t = threading.Thread(target=report_memory, args=[p.pid])
t.start()
ru = os.wait4(p.pid, 0)[2]
t.join()
print("Maximum rss %d MB" % (ru.ru_maxrss//1024,))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment