Memory Woes

Greg google at gregdunn.org
Wed Feb 26 13:19:50 EST 2003


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




More information about the Python-list mailing list