Skip to content

Tkinter Memory Leak when updating any UI elements most noticeable when updating label images, only present on windows #124364

@maj113

Description

@maj113

Bug report

Bug description:

Minimal-ish reproducible example:

import tkinter as tk
from itertools import cycle

root = tk.Tk()

# Any GIF should work
gif_path = r""

frames = []
for i in range(100):
    try:
        # Using PIL and ImageTk has no difference
        gif = tk.PhotoImage(file=gif_path, format=f'gif -index {i}') 
        frames.append(gif.subsample(1, 1).copy()) 
    except tk.TclError:
        break

label = tk.Label(root)
label.pack()

frame_cycle = cycle(frames)

def update_frame():
    frame = next(frame_cycle)
    label.config(image=frame)
    # Happens with other update times however more updates = faster memory leak
    root.after(1, update_frame)


root.after(0, update_frame)

root.mainloop()

While the leak is most noticeable when updating a label it also happens when scrolling a text widget with labels that have images

Recording.2024-09-23.203110.mp4

Here it is increasing just by scrolling over the paused gifs (static images)

Recording.2024-09-23.203836.mp4

Note that the recording is filmed at much lower fps, appearing slower than it is

I was unable to narrow the leak down, tracemalloc didn't show anything of use (possibly an issue in tcl/tk?)

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Linux, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions