Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Memory leaks on matplotlib 3.4.2 (and 3.4.0) #20490

Closed
@fohrloop

Description

@fohrloop

Bug report

Bug summary

Multiple different memory related errors when running as a part of script creating and saving lots of figures.

Code for reproduction

Sorry, could not create a minimal example that reproduces this. Just wanted to leave a note.

Actual outcome

There are multiple different errors that happen. Running the same script will produce different results randomly.

Example 1
Just prints this. No Exceptions, nothing. Script stops running.

unable to alloc 4320000 bytes

Example 2

  File "c:\myapp\calculations\myscript.py", line 422, in create_figure
    fig, ax = plt.subplots(nrows=1, figsize=(16, 9))
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\_api\deprecation.py", line 471, in wrapper
    return func(*args, **kwargs)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\pyplot.py", line 1439, in subplots
    fig = figure(**fig_kw)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\pyplot.py", line 797, in figure
    manager = new_figure_manager(
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\pyplot.py", line 316, in new_figure_manager
    return _backend_mod.new_figure_manager(*args, **kwargs)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\backend_bases.py", line 3545, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\backends\_backend_tk.py", line 899, in new_figure_manager_given_figure
    canvas = cls.FigureCanvas(figure, master=window)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\_api\deprecation.py", line 431, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\backends\_backend_tk.py", line 174, in __init__
    self._tkphoto = tk.PhotoImage(
  File "C:\Python\Python 3.8.6-32\lib\tkinter\__init__.py", line 4061, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Python\Python 3.8.6-32\lib\tkinter\__init__.py", line 4006, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: not enough free memory for image buffer

Example 3

  File "c:\myapp\calculations\myscript.py", line 467, in create_some_figures
    plt.savefig(self.folder / "myfig.png")
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\pyplot.py", line 966, in savefig
    res = fig.savefig(*args, **kwargs)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\figure.py", line 3005, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\backend_bases.py", line 2255, in print_figure
    result = print_method(
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\backend_bases.py", line 1669, in wrapper
    return func(*args, **kwargs)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\backends\backend_agg.py", line 508, in print_png
    FigureCanvasAgg.draw(self)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\backends\backend_agg.py", line 401, in draw
    self.renderer = self.get_renderer(cleared=True)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\backends\backend_agg.py", line 417, in get_renderer
    self.renderer = RendererAgg(w, h, self.figure.dpi)
  File "C:\Python\venvs\adiapp\lib\site-packages\matplotlib\backends\backend_agg.py", line 91, in __init__
    self._renderer = _RendererAgg(int(width), int(height), dpi)
MemoryError: In RendererAgg: Out of memory

Expected outcome

No errors.

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 32-bit 3.8.6

Workaround

As a side note, tried on version 3.3.4 and everything works. Does not work on 3.4.0. I'm calling

        plt.close("all")
        plt.close()
        gc.collect()

after every plt.savefig() just to be sure.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions