Skip to content

Commit b7b3445

Browse files
authored
Merge pull request #6944 from efiring/restore_report_memory
Restore cbook.report_memory, which was deleted in d063dee.
2 parents af10385 + 3dbd343 commit b7b3445

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

lib/matplotlib/cbook.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,52 @@ def restrict_dict(d, keys):
14231423
return dict([(k, v) for (k, v) in six.iteritems(d) if k in keys])
14241424

14251425

1426+
def report_memory(i=0): # argument may go away
1427+
'return the memory consumed by process'
1428+
from matplotlib.compat.subprocess import Popen, PIPE
1429+
pid = os.getpid()
1430+
if sys.platform == 'sunos5':
1431+
try:
1432+
a2 = Popen('ps -p %d -o osz' % pid, shell=True,
1433+
stdout=PIPE).stdout.readlines()
1434+
except OSError:
1435+
raise NotImplementedError(
1436+
"report_memory works on Sun OS only if "
1437+
"the 'ps' program is found")
1438+
mem = int(a2[-1].strip())
1439+
elif sys.platform.startswith('linux'):
1440+
try:
1441+
a2 = Popen('ps -p %d -o rss,sz' % pid, shell=True,
1442+
stdout=PIPE).stdout.readlines()
1443+
except OSError:
1444+
raise NotImplementedError(
1445+
"report_memory works on Linux only if "
1446+
"the 'ps' program is found")
1447+
mem = int(a2[1].split()[1])
1448+
elif sys.platform.startswith('darwin'):
1449+
try:
1450+
a2 = Popen('ps -p %d -o rss,vsz' % pid, shell=True,
1451+
stdout=PIPE).stdout.readlines()
1452+
except OSError:
1453+
raise NotImplementedError(
1454+
"report_memory works on Mac OS only if "
1455+
"the 'ps' program is found")
1456+
mem = int(a2[1].split()[0])
1457+
elif sys.platform.startswith('win'):
1458+
try:
1459+
a2 = Popen(["tasklist", "/nh", "/fi", "pid eq %d" % pid],
1460+
stdout=PIPE).stdout.read()
1461+
except OSError:
1462+
raise NotImplementedError(
1463+
"report_memory works on Windows only if "
1464+
"the 'tasklist' program is found")
1465+
mem = int(a2.strip().split()[-2].replace(',', ''))
1466+
else:
1467+
raise NotImplementedError(
1468+
"We don't have a memory monitor for %s" % sys.platform)
1469+
return mem
1470+
1471+
14261472
_safezip_msg = 'In safezip, len(args[0])=%d but len(args[%d])=%d'
14271473

14281474

0 commit comments

Comments
 (0)