Python 和 GTK:如何创建垃圾收集器友好的对象?
我开始使用 GTK+ 作为我的小部件工具包,用 Python 编写一个小型应用程序。最近,我决定进行一些内存优化,并注意到我创建的大多数 PyGTK 对象从未被垃圾收集器释放。即使我打开一扇窗户并随后适当地销毁它,这个数字仍然会继续增长。
有人能给我指出如何从 Python 创建和处理 GTK+ 对象的正确方向吗?我不使用 Glade 或类似的东西。
我还注意到创建这样的窗口:
class SomeWindow:
def __init__(self):
self.window = gtk.Window(type=gtk.WINDOW_TOPLEVEL)
而不是:
class SomeWindow(gtk.Window):
def __init__(self):
super(SomeWindow, self).__init__(type=gtk.WINDOW_TOPLEVEL)
在 GC 列表中减少 2 个以上的对象。
你能给我什么建议吗?
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.
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)
Gives 2+ objects less in GC list.
Any advices you can give me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你调用了.destroy()吗?
根据 gtk.Widget.destroy docu GTK 维护一个顶级窗口列表,该列表将使引用保持活动状态。
如果您使用任何 Python 析构函数,它们还可以防止循环引用被释放。
Did you call .destroy()?
According to the gtk.Widget.destroy docu GTK maintains a list of toplevel windows that will keep a reference alive.
And in case you use any Python destructors, they can also prevent cyclic references from being freed.