<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>

<p dir="auto">When QT5 backend style, there is a memory leaks when saving the figure in a png file. Ther is a simple example with memory output that illustrate the problem.</p>
<p dir="auto"><strong>Code for reproduction</strong></p>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib as mpl
mpl.use('Qt5Agg')
import matplotlib.pyplot as plt

import numpy as np

import gc
import sys

for i in range(10000):

    if i % 100 == 0:
        gc.collect()
        print(str(i), len( gc.get_objects()), sys.getsizeof(gc.get_objects()))

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.scatter(np.random.random(10), np.random.random(10))
    fig.savefig('test.png')

    plt.close()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">as</span> <span class="pl-s1">mpl</span>
<span class="pl-s1">mpl</span>.<span class="pl-en">use</span>(<span class="pl-s">'Qt5Agg'</span>)
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>

<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-k">import</span> <span class="pl-s1">gc</span>
<span class="pl-k">import</span> <span class="pl-s1">sys</span>

<span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">10000</span>):

    <span class="pl-k">if</span> <span class="pl-s1">i</span> <span class="pl-c1">%</span> <span class="pl-c1">100</span> <span class="pl-c1">==</span> <span class="pl-c1">0</span>:
        <span class="pl-s1">gc</span>.<span class="pl-en">collect</span>()
        <span class="pl-en">print</span>(<span class="pl-en">str</span>(<span class="pl-s1">i</span>), <span class="pl-en">len</span>( <span class="pl-s1">gc</span>.<span class="pl-en">get_objects</span>()), <span class="pl-s1">sys</span>.<span class="pl-en">getsizeof</span>(<span class="pl-s1">gc</span>.<span class="pl-en">get_objects</span>()))

    <span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">figure</span>()
    <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">add_subplot</span>(<span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>)
    <span class="pl-s1">ax</span>.<span class="pl-en">scatter</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">random</span>(<span class="pl-c1">10</span>), <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">random</span>(<span class="pl-c1">10</span>))
    <span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'test.png'</span>)

    <span class="pl-s1">plt</span>.<span class="pl-en">close</span>()</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="The is the results with differents backends:

mpl.use('Cairo')

0 43292 361288
100 45464 406496
200 45400 406496
300 45516 406496
400 45460 406496
500 45704 406496
600 45641 406496
700 45701 406496
800 45702 406496
900 45520 406496
1000 45823 406496
1100 45521 406496
1200 45701 406496
1300 45521 406496
1400 45702 406496
1500 45823 406496
1600 45462 406496
1700 45881 406496
1800 45643 406496
1900 45823 406496
2000 45639 406496
2100 45702 406496
2200 45703 406496
2300 45878 406496
2400 45520 406496
2500 45821 406496
2600 45942 406496
2700 45762 406496
2800 45402 406496
2900 45643 406496
3000 45582 406496


mpl.use('Qt5Cairo')

0 55035 457352
100 58230 514568
200 58774 514568
300 58709 514568
400 59008 514568
500 59435 514568
600 59735 514568
700 60273 514568
800 60389 514568
900 60573 514568
1000 60875 514568
1100 61353 514568
1200 61476 514568
1300 62194 514568
1400 62314 514568
1500 62552 514568
1600 62795 514568
1700 63154 514568
1800 63275 514568
1900 64054 514568
2000 64052 514568
2100 64116 514568
2200 64596 578936
2300 65192 578936
2400 64952 578936
2500 65609 578936
2600 65854 578936
2700 66151 578936
2800 66459 578936
2900 66515 578936
3000 67114 578936

mpl.use('Agg')
0 42738 361288
100 45090 361288
200 44908 361288
300 45094 361288
400 44969 361288
500 44849 361288
600 44910 361288
700 44969 361288
800 44910 361288
900 45090 361288
1000 45268 406496
1100 44969 361288
1200 44851 361288
1300 44968 361288
1400 44971 361288
1500 44850 361288
1600 45149 361288
1700 45026 361288
1800 45270 406496
1900 45329 406496
2000 44910 361288
2100 45149 361288
2200 45030 361288
2300 44849 361288
2400 45386 406496
2500 45089 361288
2600 44911 361288
2700 45095 361288
2800 44911 361288
2900 44910 361288
3000 45036 361288

mpl.use('Qt5Agg')

100 57656 514568
200 57954 514568
300 58440 514568
400 58735 514568
500 59095 514568
600 59395 514568
700 59755 514568
800 60055 514568
900 60296 514568
1000 60476 514568
1100 60716 514568
1200 61138 514568
1300 61502 514568
1400 61555 514568
1500 61914 514568
1600 62338 514568
1700 62693 514568
1800 62751 514568
1900 63358 514568
2000 63483 514568
2100 63954 514568
2200 64080 514568
2300 64498 578936
2400 64796 578936
2500 65096 578936
2600 65396 578936
2700 65520 578936
2800 66120 578936
2900 66239 578936
3000 66593 578936"><pre class="notranslate"><code class="notranslate">The is the results with differents backends:

mpl.use('Cairo')

0 43292 361288
100 45464 406496
200 45400 406496
300 45516 406496
400 45460 406496
500 45704 406496
600 45641 406496
700 45701 406496
800 45702 406496
900 45520 406496
1000 45823 406496
1100 45521 406496
1200 45701 406496
1300 45521 406496
1400 45702 406496
1500 45823 406496
1600 45462 406496
1700 45881 406496
1800 45643 406496
1900 45823 406496
2000 45639 406496
2100 45702 406496
2200 45703 406496
2300 45878 406496
2400 45520 406496
2500 45821 406496
2600 45942 406496
2700 45762 406496
2800 45402 406496
2900 45643 406496
3000 45582 406496


mpl.use('Qt5Cairo')

0 55035 457352
100 58230 514568
200 58774 514568
300 58709 514568
400 59008 514568
500 59435 514568
600 59735 514568
700 60273 514568
800 60389 514568
900 60573 514568
1000 60875 514568
1100 61353 514568
1200 61476 514568
1300 62194 514568
1400 62314 514568
1500 62552 514568
1600 62795 514568
1700 63154 514568
1800 63275 514568
1900 64054 514568
2000 64052 514568
2100 64116 514568
2200 64596 578936
2300 65192 578936
2400 64952 578936
2500 65609 578936
2600 65854 578936
2700 66151 578936
2800 66459 578936
2900 66515 578936
3000 67114 578936

mpl.use('Agg')
0 42738 361288
100 45090 361288
200 44908 361288
300 45094 361288
400 44969 361288
500 44849 361288
600 44910 361288
700 44969 361288
800 44910 361288
900 45090 361288
1000 45268 406496
1100 44969 361288
1200 44851 361288
1300 44968 361288
1400 44971 361288
1500 44850 361288
1600 45149 361288
1700 45026 361288
1800 45270 406496
1900 45329 406496
2000 44910 361288
2100 45149 361288
2200 45030 361288
2300 44849 361288
2400 45386 406496
2500 45089 361288
2600 44911 361288
2700 45095 361288
2800 44911 361288
2900 44910 361288
3000 45036 361288

mpl.use('Qt5Agg')

100 57656 514568
200 57954 514568
300 58440 514568
400 58735 514568
500 59095 514568
600 59395 514568
700 59755 514568
800 60055 514568
900 60296 514568
1000 60476 514568
1100 60716 514568
1200 61138 514568
1300 61502 514568
1400 61555 514568
1500 61914 514568
1600 62338 514568
1700 62693 514568
1800 62751 514568
1900 63358 514568
2000 63483 514568
2100 63954 514568
2200 64080 514568
2300 64498 578936
2400 64796 578936
2500 65096 578936
2600 65396 578936
2700 65520 578936
2800 66120 578936
2900 66239 578936
3000 66593 578936
</code></pre></div>
<p dir="auto">As you can see, when using backend without QT5, there is no memory leaks.</p>
<p dir="auto">Do you have an idear or solution about this problem?</p>
<p dir="auto">Thank you for your help.</p>
<p dir="auto"><strong>Expected outcome</strong></p>


<p dir="auto"><strong>Matplotlib version</strong></p>

<ul dir="auto">
<li>Operating system: Debian 9.12</li>
<li>Matplotlib version: 3.0.3</li>
<li>Matplotlib backend (<code class="notranslate">print(matplotlib.get_backend())</code>): Qt5agg, agg, Qt5Cairo, cairo</li>
<li>Python version: 3.5</li>
<li>Jupyter version (if applicable):</li>
<li>Other libraries:</li>
</ul>


<p dir="auto">python from debian repository<br>
matplotlib from  pip</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="22:39 $ python unit/memleak.py Qt5Agg 50 out.png
   0: pymalloc   17627699, rss  116760576, nobjs      55869, garbage    0, files:    2
   1: pymalloc   17634545, rss  117694464, nobjs      55872, garbage    0, files:    2
   2: pymalloc   17635196, rss  118116352, nobjs      55875, garbage    0, files:    2
   3: pymalloc   17635615, rss  118386688, nobjs      55878, garbage    0, files:    2
   4: pymalloc   17636002, rss  118386688, nobjs      55881, garbage    0, files:    2
   5: pymalloc   17636389, rss  118386688, nobjs      55884, garbage    0, files:    2
   6: pymalloc   17636776, rss  118386688, nobjs      55887, garbage    0, files:    2
   7: pymalloc   17637176, rss  118386688, nobjs      55890, garbage    0, files:    2
   8: pymalloc   17637579, rss  118386688, nobjs      55893, garbage    0, files:    2
   9: pymalloc   17637985, rss  118386688, nobjs      55896, garbage    0, files:    2
  10: pymalloc   17638340, rss  118386688, nobjs      55899, garbage    0, files:    2
  11: pymalloc   17638791, rss  118386688, nobjs      55902, garbage    0, files:    2
  12: pymalloc   17639162, rss  118386688, nobjs      55905, garbage    0, files:    2
  13: pymalloc   17639533, rss  118386688, nobjs      55908, garbage    0, files:    2
  14: pymalloc   17639920, rss  118386688, nobjs      55911, garbage    0, files:    2
  15: pymalloc   17640307, rss  118386688, nobjs      55914, garbage    0, files:    2
  16: pymalloc   17640694, rss  118386688, nobjs      55917, garbage    0, files:    2
  17: pymalloc   17641081, rss  118386688, nobjs      55920, garbage    0, files:    2
  18: pymalloc   17641468, rss  118386688, nobjs      55923, garbage    0, files:    2
  19: pymalloc   17641902, rss  118386688, nobjs      55926, garbage    0, files:    2
  20: pymalloc   17642323, rss  118386688, nobjs      55929, garbage    0, files:    2
  21: pymalloc   17642573, rss  118386688, nobjs      55932, garbage    0, files:    2
  22: pymalloc   17643421, rss  118386688, nobjs      55935, garbage    0, files:    2
  23: pymalloc   17643987, rss  118386688, nobjs      55938, garbage    0, files:    2
  24: pymalloc   17644342, rss  118386688, nobjs      55941, garbage    0, files:    2
  25: pymalloc   17644745, rss  118386688, nobjs      55944, garbage    0, files:    2
  26: pymalloc   17645132, rss  118386688, nobjs      55947, garbage    0, files:    2
  27: pymalloc   17645519, rss  118386688, nobjs      55950, garbage    0, files:    2
  28: pymalloc   17645906, rss  118386688, nobjs      55953, garbage    0, files:    2
  29: pymalloc   17646340, rss  118386688, nobjs      55956, garbage    0, files:    2
  30: pymalloc   17646816, rss  118386688, nobjs      55959, garbage    0, files:    2
  31: pymalloc   17647075, rss  118386688, nobjs      55962, garbage    0, files:    2
  32: pymalloc   17647470, rss  118386688, nobjs      55965, garbage    0, files:    2
  33: pymalloc   17647857, rss  118386688, nobjs      55968, garbage    0, files:    2
  34: pymalloc   17648244, rss  118386688, nobjs      55971, garbage    0, files:    2
  35: pymalloc   17648631, rss  118386688, nobjs      55974, garbage    0, files:    2
  36: pymalloc   17649049, rss  118386688, nobjs      55977, garbage    0, files:    2
  37: pymalloc   17649461, rss  118386688, nobjs      55980, garbage    0, files:    2
  38: pymalloc   17649784, rss  118386688, nobjs      55983, garbage    0, files:    2
  39: pymalloc   17650115, rss  118386688, nobjs      55986, garbage    0, files:    2
  40: pymalloc   17650531, rss  118386688, nobjs      55989, garbage    0, files:    2
  41: pymalloc   17651001, rss  118386688, nobjs      55992, garbage    0, files:    2
  42: pymalloc   17651260, rss  118386688, nobjs      55995, garbage    0, files:    2
  43: pymalloc   17651727, rss  118386688, nobjs      55998, garbage    0, files:    2
  44: pymalloc   17652114, rss  118386688, nobjs      56001, garbage    0, files:    2
  45: pymalloc   17652501, rss  118386688, nobjs      56004, garbage    0, files:    2
  46: pymalloc   17652888, rss  118386688, nobjs      56007, garbage    0, files:    2
  47: pymalloc   17653195, rss  118386688, nobjs      56010, garbage    0, files:    2
  48: pymalloc   17653611, rss  118386688, nobjs      56013, garbage    0, files:    2
  49: pymalloc   17654065, rss  118657024, nobjs      56016, garbage    0, files:    2
Average memory consumed per loop: 10813.4400 bytes"><pre class="notranslate"><code class="notranslate">22:39 $ python unit/memleak.py Qt5Agg 50 out.png
   0: pymalloc   17627699, rss  116760576, nobjs      55869, garbage    0, files:    2
   1: pymalloc   17634545, rss  117694464, nobjs      55872, garbage    0, files:    2
   2: pymalloc   17635196, rss  118116352, nobjs      55875, garbage    0, files:    2
   3: pymalloc   17635615, rss  118386688, nobjs      55878, garbage    0, files:    2
   4: pymalloc   17636002, rss  118386688, nobjs      55881, garbage    0, files:    2
   5: pymalloc   17636389, rss  118386688, nobjs      55884, garbage    0, files:    2
   6: pymalloc   17636776, rss  118386688, nobjs      55887, garbage    0, files:    2
   7: pymalloc   17637176, rss  118386688, nobjs      55890, garbage    0, files:    2
   8: pymalloc   17637579, rss  118386688, nobjs      55893, garbage    0, files:    2
   9: pymalloc   17637985, rss  118386688, nobjs      55896, garbage    0, files:    2
  10: pymalloc   17638340, rss  118386688, nobjs      55899, garbage    0, files:    2
  11: pymalloc   17638791, rss  118386688, nobjs      55902, garbage    0, files:    2
  12: pymalloc   17639162, rss  118386688, nobjs      55905, garbage    0, files:    2
  13: pymalloc   17639533, rss  118386688, nobjs      55908, garbage    0, files:    2
  14: pymalloc   17639920, rss  118386688, nobjs      55911, garbage    0, files:    2
  15: pymalloc   17640307, rss  118386688, nobjs      55914, garbage    0, files:    2
  16: pymalloc   17640694, rss  118386688, nobjs      55917, garbage    0, files:    2
  17: pymalloc   17641081, rss  118386688, nobjs      55920, garbage    0, files:    2
  18: pymalloc   17641468, rss  118386688, nobjs      55923, garbage    0, files:    2
  19: pymalloc   17641902, rss  118386688, nobjs      55926, garbage    0, files:    2
  20: pymalloc   17642323, rss  118386688, nobjs      55929, garbage    0, files:    2
  21: pymalloc   17642573, rss  118386688, nobjs      55932, garbage    0, files:    2
  22: pymalloc   17643421, rss  118386688, nobjs      55935, garbage    0, files:    2
  23: pymalloc   17643987, rss  118386688, nobjs      55938, garbage    0, files:    2
  24: pymalloc   17644342, rss  118386688, nobjs      55941, garbage    0, files:    2
  25: pymalloc   17644745, rss  118386688, nobjs      55944, garbage    0, files:    2
  26: pymalloc   17645132, rss  118386688, nobjs      55947, garbage    0, files:    2
  27: pymalloc   17645519, rss  118386688, nobjs      55950, garbage    0, files:    2
  28: pymalloc   17645906, rss  118386688, nobjs      55953, garbage    0, files:    2
  29: pymalloc   17646340, rss  118386688, nobjs      55956, garbage    0, files:    2
  30: pymalloc   17646816, rss  118386688, nobjs      55959, garbage    0, files:    2
  31: pymalloc   17647075, rss  118386688, nobjs      55962, garbage    0, files:    2
  32: pymalloc   17647470, rss  118386688, nobjs      55965, garbage    0, files:    2
  33: pymalloc   17647857, rss  118386688, nobjs      55968, garbage    0, files:    2
  34: pymalloc   17648244, rss  118386688, nobjs      55971, garbage    0, files:    2
  35: pymalloc   17648631, rss  118386688, nobjs      55974, garbage    0, files:    2
  36: pymalloc   17649049, rss  118386688, nobjs      55977, garbage    0, files:    2
  37: pymalloc   17649461, rss  118386688, nobjs      55980, garbage    0, files:    2
  38: pymalloc   17649784, rss  118386688, nobjs      55983, garbage    0, files:    2
  39: pymalloc   17650115, rss  118386688, nobjs      55986, garbage    0, files:    2
  40: pymalloc   17650531, rss  118386688, nobjs      55989, garbage    0, files:    2
  41: pymalloc   17651001, rss  118386688, nobjs      55992, garbage    0, files:    2
  42: pymalloc   17651260, rss  118386688, nobjs      55995, garbage    0, files:    2
  43: pymalloc   17651727, rss  118386688, nobjs      55998, garbage    0, files:    2
  44: pymalloc   17652114, rss  118386688, nobjs      56001, garbage    0, files:    2
  45: pymalloc   17652501, rss  118386688, nobjs      56004, garbage    0, files:    2
  46: pymalloc   17652888, rss  118386688, nobjs      56007, garbage    0, files:    2
  47: pymalloc   17653195, rss  118386688, nobjs      56010, garbage    0, files:    2
  48: pymalloc   17653611, rss  118386688, nobjs      56013, garbage    0, files:    2
  49: pymalloc   17654065, rss  118657024, nobjs      56016, garbage    0, files:    2
Average memory consumed per loop: 10813.4400 bytes
</code></pre></div>