8000 Use psutil instead of our home-grown utilities · matplotlib/matplotlib@b59627b · GitHub
[go: up one dir, main page]

Skip to content

Commit b59627b

Browse files
committed
Use psutil instead of our home-grown utilities
1 parent 457fd94 commit b59627b

File tree

2 files changed

+28
-103
lines changed

2 files changed

+28
-103
lines changed

lib/matplotlib/cbook.py

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,42 +1438,6 @@ def restrict_dict(d, keys):
14381438
return dict([(k, v) for (k, v) in six.iteritems(d) if k in keys])
14391439

14401440

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

14791443

@@ -1505,58 +1469,6 @@ def safe_masked_invalid(x):
15051469
return xm
15061470

15071471

1508-
class MemoryMonitor(object):
1509-
def __init__(self, nmax=20000):
1510-
self._nmax = nmax
1511-
self._mem = np.zeros((self._nmax,), np.int32)
1512-
self.clear()
1513-
1514-
def clear(self):
1515-
self._n = 0
1516-
self._overflow = False
1517-
1518-
def __call__(self):
1519-
mem = report_memory()
1520-
if self._n < self._nmax:
1521-
self._mem[self._n] = mem
1522-
self._n += 1
1523-
else:
1524-
self._overflow = True
1525-
return mem
1526-
1527-
def report(self, segments=4):
1528-
n = self._n
1529-
segments = min(n, segments)
1530-
dn = int(n / segments)
1531-
ii = list(xrange(0, n, dn))
1532-
ii[-1] = n - 1
1533-
print()
1534-
print('memory report: i, mem, dmem, dmem/nloops')
1535-
print(0, self._mem[0])
1536-
for i in range(1, len(ii)):
1537-
di = ii[i] - ii[i - 1]
1538-
if di == 0:
1539-
continue
1540-
dm = self._mem[ii[i]] - self._mem[ii[i - 1]]
1541-
print('%5d %5d %3d %8.3f' % (ii[i], self._mem[ii[i]],
1542-
dm, dm / float(di)))
1543-
if self._overflow:
1544-
print("Warning: array size was too small for the number of calls.")
1545-
1546-
def xy(self, i0=0, isub=1):
1547-
x = np.arange(i0, self._n, isub)
1548-
return x, self._mem[i0:self._n:isub]
1549-
1550-
def plot(self, i0=0, isub=1, fig=None):
1551-
if fig is None:
1552-
from .pylab import figure
1553-
fig = figure()
1554-
1555-
ax = fig.add_subplot(111)
1556-
ax.plot(*self.xy(i0, isub))
1557-
fig.canvas.draw()
1558-
1559-
15601472
def print_cycles(objects, outstream=sys.stdout, show_progress=False):
15611473
"""
15621474
*objects*

unit/memleak.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,48 @@
33
from __future__ import print_function
44

55
import gc
6+
67
try:
78
import tracemalloc
89
except ImportError:
910
raise ImportError("This script requires Python 3.4 or later")
1011

12+
try:
13+
import psutil
14+
except ImportError:
15+
raise ImportError("This script requires psutil")
16+
1117
import numpy as np
1218

1319

1420
def run_memleak_test(bench, iterations, report):
15-
from matplotlib.cbook import report_memory
16-
1721
tracemalloc.start()
1822

19-
starti = min(10, iterations / 2)
23+
starti = min(50, iterations / 2)
2024
endi = iterations
2125

2226
malloc_arr = np.empty((endi,), dtype=np.int64)
2327
rss_arr = np.empty((endi,), dtype=np.int64)
2428
rss_peaks = np.empty((endi,), dtype=np.int64)
2529
nobjs_arr = np.empty((endi,), dtype=np.int64)
2630
garbage_arr = np.empty((endi,), dtype=np.int64)
31+
open_files_arr = np.empty((endi,), dtype=np.int64)
2732
rss_peak = 0
2833

34+
p = psutil.Process()
35+
2936
for i in range(endi):
3037
bench()
3138

3239
gc.collect()
33-
rss = report_memory()
40+
41+
rss = p.memory_info().rss
3442
malloc, peak = tracemalloc.get_traced_memory()
3543
nobjs = len(gc.get_objects())
3644
garbage = len(gc.garbage)
37-
print("{0: 4d}: pymalloc {1: 10d}, rss {2: 10d}, nobjs {3: 10d}, garbage {4: 10d}".format(
38-
i, malloc, rss, nobjs, garbage))
45+
open_files = len(p.open_files())
46+
print("{0: 4d}: pymalloc {1: 10d}, rss {2: 10d}, nobjs {3: 10d}, garbage {4: 4d}, files: {5: 4d}".format(
47+
i, malloc, rss, nobjs, garbage, open_files))
3948

4049
malloc_arr[i] = malloc
4150
rss_arr[i] = rss
@@ -44,23 +53,27 @@ def run_memleak_test(bench, iterations, report):
4453
rss_peaks[i] = rss_peak
4554
nobjs_arr[i] = nobjs
4655
garbage_arr[i] = garbage
56+
open_files_arr[i] = open_files
4757

4858
print('Average memory consumed per loop: %1.4f bytes\n' %
4959
(np.sum(rss_peaks[starti+1:] - rss_peaks[starti:-1]) / float(endi - starti)))
5060

5161
from matplotlib import pyplot as plt
52-
fig, (ax1, ax2) = plt.subplots(2)
53-
ax3 = ax1.twinx()
54-
ax1.plot(malloc_arr[5:], 'r')
55-
ax3.plot(rss_arr[5:], 'b')
62+
fig, (ax1, ax2, ax3) = plt.subplots(3)
63+
ax1b = ax1.twinx()
64+
ax1.plot(malloc_arr, 'r')
65+
ax1b.plot(rss_arr, 'b')
5666
ax1.set_ylabel('pymalloc', color='r')
57-
ax3.set_ylabel('rss', color='b')
67+
ax1b.set_ylabel('rss', color='b')
5868

59-
ax4 = ax2.twinx()
60-
ax2.plot(nobjs_arr[5:], 'r')
61-
ax4.plot(garbage_arr[5:], 'b')
69+
ax2b = ax2.twinx()
70+
ax2.plot(nobjs_arr, 'r')
71+
ax2b.plot(garbage_arr, 'b')
6272
ax2.set_ylabel('total objects', color='r')
63-
ax4.set_ylabel('garbage objects', color='b')
73+
ax2b.set_ylabel('garbage objects', color='b')
74+
75+
ax3.plot(open_files_arr)
76+
ax3.set_ylabel('open file handles')
6477

6578
if not report.endswith('.pdf'):
6679
report = report + '.pdf'

0 commit comments

Comments
 (0)
0