Memory Woes

Andy Jewell andy at wild-flower.co.uk
Thu Feb 27 21:41:15 CET 2003


On Wednesday 26 Feb 2003 6:19 pm, Greg wrote:
> I am using Python 2.2 to write a chart that plots realtime data
> similar to the one on the performance tab of the Windows task manager.
>
> Here's the problem:
>   I use a Tkinter canvas widget to display the data, and on each
> update (1/sec), the data and x gridlines scroll to the left.  If I
> don't delete any items from the canvas, the memory usage (as reported
> by the task manager) increases slowly.  This is understandable, since
> the canvas is still keeping track of the items that are no longer
> visible.  When I attempted to remedy this by deleting the items as
> they scroll off the chart, however, it began eating memory about 5
> times faster.
>
> Here's the code I use to search and destroy items that are scrolling
> off the chart:
>
>   # every item that scrolls along the x axis is given a "deleteMe"
>   # tag upon creation
>   for i in self.canvas.find_withtag("deleteMe"):
>     # margins[3] represents the left margin (the x coordinate of
>     # the y axis)
>     if self.canvas.coords(i)[0] <= self.margins[3]:
>       self.canvas.delete(i)
>
> I've read quite a few posts that suggest memory leaks with callback
> bindings in older versions of tk, but I don't have any bindings
> attached to any of the deleted items, and "deleteMe" is the only tag
> associated with any of them.
>
> This script is meant to run over long periods of time, and the memory
> usage will become a major issue as time goes on.  Any thoughts as to
> why this is happening/how to fix it?  I'm fairly new to python and to
> gui programming, so feel free to let me know if I'm doing something
> completely idiotic.
>
> Thanks,
>   Greg

Greg,

Is your canvas object of known, and maybe predetermined dimensions?

If so, why not just keep a list of points or vectors and *move* them in 
rotation, re-using objects that have scrolled off the left, rather than 
constantly creating new objects?  This would probably be faster too.

By the way, if you keep scrolling the canvas left (or any other direction for 
that matter) won't you eventually hit the dimensions limit for canvas 
objects?

I obviously don't know what hardware you're running on, but I found Tkinter to 
work faster than I expected on my PII/300, admittedly with a reasonable 
graphics card.

Good luck,

-andyj







More information about the Python-list mailing list