I'm using the latest release of the PyGTK All-in-One installer (2.24.2) for Python 2.7 which includes Cairo 1.10.8, Pango 1.29.4, PyGTK 2.24.0, and PyGobject 2.28.3 (I think).
The following code leaks ~55 MB of memory:
import gtk
window = gtk.Window()
label = gtk.Label()
window.add(label)
window.show_all()
for _ in range(100000):
label.set_markup('Leaking memory!')
while gtk.events_pending():
gtk.main_iteration()
Note: The for loop is in my test script just so I could see the memory consumption increase in Task Manager. It's also essentially what's going on in my real application, except the label text changes at least once per second instead of just being redrawn with the same text each time.
The problem line is label.set_markup()
, which leaks about 0.5kB per call so I suspect the problem is in GTK or Cairo somewhere. It's possibly this bug (685959), as pointed out by a commenter.
I tried using objgraph to see if any extra Python objects are showing up in proportion to the number of calls to gtk.Label.set_markup()
but there are no excess objects. It follows that calls to gc.collect()
don't help, and I tried it to be sure. Python doesn't seem to be aware of the objects which are responsible for the memory consumption.
How do I find this memory leak, and/or work around it? I need to use markup to style some text for this application, but I tried using gtk.Label.set_text()
as a workaround and it leaks memory too.
I should note that this application targets Windows so using PyGObject to get GTK 3 is not an option -- GObject introspection is still not available on Windows.
set_markup()
suffices to show that we're leaking memory. – Maes