Python 和 GTK:如何创建垃圾收集器友好的对象?

发布于 2024-11-26 21:37:29 字数 542 浏览 2 评论 0原文

我开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2024-12-03 21:37:29

你调用了.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文