Python和GTK +:如何创建垃圾回收器友好对象? [英] Python and GTK+: How to create garbage collector friendly objects?
问题描述
我开始使用GTK +编写一个Python应用程序小工具。最近我决定做一些内存优化,并注意到我创建的大部分PyGTK对象从来都不会被垃圾收集器释放。这个数字不断增长,即使我打开一个窗口并在之后妥善销毁。
I started writing a small application in Python with GTK+ as my widget toolkit. Recently I decided to do some memory optimization and noticed that a most of PyGTK objects I create never gets released by garbage collector. This number keeps growing even if I open a window and properly destroy it afterward.
有人能指出我如何从Python创建和处理GTK +对象。我没有使用Glade或类似的东西。
Can someone point me in the right direction on how to create and handle GTK+ objects from Python. Am not using Glade or anything like that.
另外我注意到创建这样的窗口:
Also I noticed that creating window like this:
class SomeWindow:
def __init__(self):
self.window = gtk.Window(type=gtk.WINDOW_TOPLEVEL)
而不是:
Instead of:
class SomeWindow(gtk.Window):
def __init__(self):
super(SomeWindow, self).__init__(type=gtk.WINDOW_TOPLEVEL)
您可以给我的任何建议吗?
Any advices you can give me?
推荐答案
您是否调用过.destroy()?
Did you call .destroy()?
According to the gtk.Widget.destroy docu GTK maintains a list of toplevel windows that will keep a reference alive.
在使用任何Python析构函数的情况下,它们也可以防止循环引用被释放。
And in case you use any Python destructors, they can also prevent cyclic references from being freed.
这篇关于Python和GTK +:如何创建垃圾回收器友好对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!