Question

Background:

A few months ago, I posted here asking about a memory leak occurring in my Tkinter canvas class. The primary cause of this was an accidental loop over several key bindings, and the removal of these bindings largely alleviated my problem. However, there remained a small memory leak, but I disregarded it as negligible enough to ignore. At this point (after a couple more months of work), the previously small leak has grown to be potentially damaging -- after an hour of continuous use my program can now fill 800mb+ of system memory, which is a serious concern as without the leak, the program should only require ~60mb.

The Issue:

After some testing, I have narrowed the origin down to two causes:
1. Tkinter itemconfigure function
2. Tkinter text objects
The code snippet below recreates the issue on my machine.

from Tkinter import *

stop=0

class Interface:
    def Initialise(self,master):
        global x
        self.master=master
        x=50
        width,height=master.winfo_screenwidth(),master.winfo_screenheight()  
        self.c=Canvas(self.master, width=width, height=height, bg='white',scrollregion=(-1500,-1500,10000,10000))
        self.c.pack(side=LEFT,expand=True,fill=BOTH)
        self.Regenerate()
        self.master.bind("<a>", self.auto)

    def Regenerate(self):
        self.G=UI_Canvas()
        self.G.Visualise(self.c,x)

    def auto(self, event):
        global stop
        stop+=1
        self.master.after(5, self.auto_rotate)

    def auto_rotate(self):
        global x
        if stop % 2 == 1:
            x+=0.1
            self.Regenerate()
            self.c.after(5, self.auto_rotate)

class UI_Canvas:
    def Visualise(self, canvas,x):

        self.canvas=canvas
        self.canvas.delete('Network')

        data=[((x,400,0),1),((x,450,0),2),((x+50,450,0),3),((x+50,400,0),4)] 
        for (x,y,z), j in data:
            self.canvas.create_oval(x-5, y-5, x+5, y+5, fill='gray15', tags=(j,("Node %.5s" % j),'Network'))
            #self.canvas.create_text(x-10,y, text=(j), fill='black', font=('Helvetica',12), tags=(j, ("Node %.5s" % j), 'Network'))

root=Tk()
UI=Interface()
UI.Initialise(root)
root.mainloop()

By commenting out all text and itemconfigure objects, the leak disappears and the program runs at a constant memory usage (as required). Specifically, commenting out all text objects (leaving no objects to be configured), removes the leak, and it therefore seems to me that the repeated refresh of text on-screen is causing the leak. Other canvas objects (lines, ovals and polygons) that I use do not cause this issue.

Attempted Solutions

Other memory leak problems posted here seem to revolve around having updates within loops, but I have not relied on this method, and instead delete then re-create objects inside a loop. As previously mentioned, this method works fine with shapes, but apparently not with text. I have moved the text object code into my 'master' canvas class (thereby no longer being directly in a refresh loop), and instead attempted to configure it from the loop, but the issue persists.

Question

The information contained in these text objects is very important to easily using my program, and resolving this issue is essential. My questions are: what is causing this issue? And are there any possible solutions?

Thank you in advance for any assistance!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top