8000 test figure creation memory leak · matplotlib/matplotlib@2056ce1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2056ce1

Browse files
test figure creation memory leak
1 parent 342c8d9 commit 2056ce1

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies:
4747
- nbformat!=5.0.0,!=5.0.1
4848
- pandas!=0.25.0
4949
- pikepdf
50+
- psutil
5051
- pre-commit
5152
- pydocstyle>=5.1.0
5253
- pytest!=4.6.0,!=5.4.0

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,46 @@ def test_blitting_events(env):
503503
# blitting is not properly implemented
504504
ndraws = proc.stdout.count("DrawEvent")
505505
assert 0 < ndraws < 5
506+
507+
508+
# The source of this function gets extracted and run in another process, so it
509+
# must be fully self-contained.
510+
def _test_figure_leak():
511+
import sys
512+
513+
import psutil
514+
from matplotlib import pyplot as plt
515+
# Second argument is pause length, but if zero we should skip pausing
516+
t = float(sys.argv[1])
517+
518+
# Warmup cycle, this reasonably allocates a lot
519+
p = psutil.Process()
520+
fig = plt.figure()
521+
if t:
522+
plt.pause(t)
523+
plt.close(fig)
524+
mem = p.memory_full_info().uss
525+
526+
for _ in range(5):
527+
fig = plt.figure()
528+
if t:
529+
plt.pause(t)
530+
plt.close(fig)
531+
growth = p.memory_full_info().uss - mem
532+
533+
print(growth)
534+
535+
536+
@pytest.mark.parametrize("env", _get_testable_interactive_backends())
537+
@pytest.mark.parametrize("time", ["0.0", "0.1"])
538+
def test_figure_leak_20490(env, time):
539+
pytest.importorskip("psutil", reason="psutil needed to run this test")
540+
541+
# We can't yet directly identify the leak
542+
# so test with a memory growth threshold
543+
acceptable_memory_leakage = 2_000_000
544+
545+
result = _run_helper(_test_figure_leak, time, timeout=_test_timeout, **env)
546+
547+
growth = int(result.stdout)
548+
assert growth <= acceptable_memory_leakage

requirements/testing/all.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
certifi
44
coverage<6.3
5+
psutil
56
pytest!=4.6.0,!=5.4.0
67
pytest-cov
78
pytest-rerunfailures

0 commit comments

Comments
 (0)
0