MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / run_memleak_test

Function run_memleak_test

tools/memleak.py:15–85  ·  view source on GitHub ↗
(bench, iterations, report)

Source from the content-addressed store, hash-verified

13
14
15def run_memleak_test(bench, iterations, report):
16 tracemalloc.start()
17
18 starti = min(50, iterations // 2)
19 endi = iterations
20
21 malloc_arr = np.empty(endi, dtype=np.int64)
22 rss_arr = np.empty(endi, dtype=np.int64)
23 rss_peaks = np.empty(endi, dtype=np.int64)
24 nobjs_arr = np.empty(endi, dtype=np.int64)
25 garbage_arr = np.empty(endi, dtype=np.int64)
26 open_files_arr = np.empty(endi, dtype=np.int64)
27 rss_peak = 0
28
29 p = psutil.Process()
30
31 for i in range(endi):
32 bench()
33
34 gc.collect()
35
36 rss = p.memory_info().rss
37 malloc, peak = tracemalloc.get_traced_memory()
38 nobjs = len(gc.get_objects())
39 garbage = len(gc.garbage)
40 open_files = len(p.open_files())
41 print(f"{i: 4d}: pymalloc {malloc: 10d}, rss {rss: 10d}, "
42 f"nobjs {nobjs: 10d}, garbage {garbage: 4d}, "
43 f"files: {open_files: 4d}")
44 if i == starti:
45 print(f'{" warmup done ":-^86s}')
46 malloc_arr[i] = malloc
47 rss_arr[i] = rss
48 if rss > rss_peak:
49 rss_peak = rss
50 rss_peaks[i] = rss_peak
51 nobjs_arr[i] = nobjs
52 garbage_arr[i] = garbage
53 open_files_arr[i] = open_files
54
55 print('Average memory consumed per loop: {:1.4f} bytes\n'.format(
56 np.sum(rss_peaks[starti+1:] - rss_peaks[starti:-1]) / (endi - starti)))
57
58 from matplotlib import pyplot as plt
59 from matplotlib.ticker import EngFormatter
60 bytes_formatter = EngFormatter(unit='B')
61 fig, (ax1, ax2, ax3) = plt.subplots(3)
62 for ax in (ax1, ax2, ax3):
63 ax.axvline(starti, linestyle='--', color='k')
64 ax1b = ax1.twinx()
65 ax1b.yaxis.set_major_formatter(bytes_formatter)
66 ax1.plot(malloc_arr, 'C0')
67 ax1b.plot(rss_arr, 'C1', label='rss')
68 ax1b.plot(rss_peaks, 'C1', linestyle='--', label='rss max')
69 ax1.set_ylabel('pymalloc', color='C0')
70 ax1b.set_ylabel('rss', color='C1')
71 ax1b.legend()
72

Callers 1

memleak.pyFile · 0.85

Calls 11

EngFormatterClass · 0.90
axvlineMethod · 0.80
set_ylabelMethod · 0.80
startMethod · 0.45
subplotsMethod · 0.45
twinxMethod · 0.45
set_major_formatterMethod · 0.45
plotMethod · 0.45
legendMethod · 0.45
tight_layoutMethod · 0.45
savefigMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…