From 3dbd343303fd17324833db88c177aca94f4c0d44 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Fri, 12 Aug 2016 10:04:48 -1000 Subject: [PATCH] Restore cbook.report_memory, which was deleted in d063dee. There was no deprecation process, and having something like this available as an import is very valuable to users suspecting a memory leak, or simply needing to track memory usage. --- lib/matplotlib/cbook.py | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 5556a65d4c09..aa9a0dfcfb0c 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -1423,6 +1423,52 @@ def restrict_dict(d, keys): return dict([(k, v) for (k, v) in six.iteritems(d) if k in keys]) +def report_memory(i=0): # argument may go away + 'return the memory consumed by process' + from matplotlib.compat.subprocess import Popen, PIPE + pid = os.getpid() + if sys.platform == 'sunos5': + try: + a2 = Popen('ps -p %d -o osz' % pid, shell=True, + stdout=PIPE).stdout.readlines() + except OSError: + raise NotImplementedError( + "report_memory works on Sun OS only if " + "the 'ps' program is found") + mem = int(a2[-1].strip()) + elif sys.platform.startswith('linux'): + try: + a2 = Popen('ps -p %d -o rss,sz' % pid, shell=True, + stdout=PIPE).stdout.readlines() + except OSError: + raise NotImplementedError( + "report_memory works on Linux only if " + "the 'ps' program is found") + mem = int(a2[1].split()[1]) + elif sys.platform.startswith('darwin'): + try: + a2 = Popen('ps -p %d -o rss,vsz' % pid, shell=True, + stdout=PIPE).stdout.readlines() + except OSError: + raise NotImplementedError( + "report_memory works on Mac OS only if " + "the 'ps' program is found") + mem = int(a2[1].split()[0]) + elif sys.platform.startswith('win'): + try: + a2 = Popen(["tasklist", "/nh", "/fi", "pid eq %d" % pid], + stdout=PIPE).stdout.read() + except OSError: + raise NotImplementedError( + "report_memory works on Windows only if " + "the 'tasklist' program is found") + mem = int(a2.strip().split()[-2].replace(',', '')) + else: + raise NotImplementedError( + "We don't have a memory monitor for %s" % sys.platform) + return mem + + _safezip_msg = 'In safezip, len(args[0])=%d but len(args[%d])=%d'