Skip to content

[Bug]: FigureCanvasTkAgg call creates memory leak #22858

@AlbertoEusebio

Description

@AlbertoEusebio

Bug summary

python 3.10.4

While using matplotlib 3.5.1 I have encountered a problem in calling multiple times the function FigureCanvasTkAgg().

Code for reproduction

from tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tracemalloc
import gc


def repeat(n, fig, root):
    for i in range(n):
        agg = FigureCanvasTkAgg(fig, root).get_tk_widget()
        agg.destroy()
        del agg
        gc.collect()


def main():
    root = Tk()
    root.geometry("300x300+0+0")
    fig = Figure(dpi=200)

    tracemalloc.start()
    tracemalloc.clear_traces()

    print(tracemalloc.get_traced_memory())
    repeat(10, fig, root)
    print(tracemalloc.get_traced_memory())

    tracemalloc.clear_traces()
    print(tracemalloc.get_traced_memory())

    repeat(20, fig, root)
    print(tracemalloc.get_traced_memory())

    tracemalloc.clear_traces()
    print(tracemalloc.get_traced_memory())

    repeat(30, fig, root)
    print(tracemalloc.get_traced_memory())

    tracemalloc.clear_traces()
    print(tracemalloc.get_traced_memory())

    root.mainloop()


if __name__ == "__main__":
    main()

Actual outcome

(0, 0)
(42116, 55520)
(0, 0)
(41983, 55387)
(0, 0)
(65031, 78436)
(0, 0)

Expected outcome

(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)

Additional information

No response

Operating system

Windows 10

Matplotlib Version

3.5.1

Matplotlib Backend

module://backend_interagg

Python version

3.10.4

Jupyter version

No response

Installation

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy