Skip to content

Memory leak in plt.close() when unshown figures in GUI backends are closed #20300

@P-Takenaka

Description

@P-Takenaka

Bug report

Bug summary

When creating multiple plots (e.g. in a loop), the allocated memory for the process increases in each iteration even though the figure is cleared each time.

Code for reproduction

import gc
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
for i in range(5000):
    fig.clf()
    plt.cla()
    ax.plot([0,1,2,3,4,5], [0,1,2,3,4,5])
    plt.close('all')
    plt.close(fig)

    gc.collect()

Expected outcome

The allocated memory should not increase in each iteration.

Matplotlib version

  • Operating system: Arch Linux
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.9.5
  • Jupyter version (if applicable):
  • Other libraries:

Matplotlib was installed via pip.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions