pyqt:内存使用 [英] pyqt: Memory Usage

查看:37
本文介绍了pyqt:内存使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,它在其中创建选项卡,每次我创建一个选项卡时,内存使用量都会增加,但是当我关闭选项卡时,内存不会减少.我只是想知道,这是否正常,我能做些什么吗?这是我关闭标签的代码:

I have a program and in it, it creates tabs, and every time I create one the memory usage goes up, but when I close a tab out the memory doesn't go down. I am just wondering, is this normal, and is there something I can do about it? Here is my code for closing out tabs:

def removeTab(self, index):

  text[index].deleteLater()
  del text[index] # text box

  # reorganize dict
  last = -1
  for key in sorted(text):
    if last+1 != key:
      text[key-1] = text[key]
      del text[key]
    last += 1

  self.tab_widget.setCurrentIndex(index)
  widget = self.tab_widget.currentWidget()
  self.tab_widget.removeTab(index)  # remove tab
  widget.deleteLater()
  del widget

推荐答案

粗略地说:

  1. python 应用程序可用的内存量可以增长在它的生命周期中,但它永远不能完全收缩回原来的位置开始了.

  1. The amount of memory available to a python application can grow during its lifetime, but it can never fully shrink back to where it started.

没有办法强制python将未使用的内存释放回系统.

There is no way to force python to release unused memory back to the system.

所以你能做的最好的事情就是尝试管理内存增长的数量.一种明显的方法是删除不需要的对象,以便新创建的对象可以重新使用已经分配的内存.

So the best you can do is try to manage the amount by which the memory grows. One obvious way to do this is to delete unneeded objects, so that newly created objects can re-use the memory that has already been allocated.

但是如果应用程序看起来无法重用内存怎么办?这要么意味着对象没有被干净地删除(即有对它们的引用被保存在某处),或者,不太可能,PyQt/Qt 中存在潜在的内存泄漏.

But what if the application seems unable to re-use the memory? This either means that objects are not being deleted cleanly (i.e. there are references to them being kept somewhere), or, much less likely, that there is an underlying memory leak in PyQt/Qt.

追踪保留给对象的额外引用有时会非常棘手(至少可以这么说).PyQt 中一些常见的隐藏位置"是使用 lambda(或 functools.partial)或猴子补丁保护方法连接的信号.但是找到它们并没有硬性规定.

Tracking down extra references kept to objects can sometimes be quite tricky (to say the least). Some common "hiding places" in PyQt are signals connected using lambda (or functools.partial), or monkey-patched protected methods. But there are no hard-and-fast rules for finding them.

就目前而言,问题中的示例代码似乎没有任何可以隐藏"额外引用的地方.因此,如果每次"创建一个选项卡时内存使用量都会增加(正如 OP 所声称的那样),那么问题一定出在应用程序的其他地方——或者更准确地说,问题可能在于对象的 创建,而不是删除它们的方式.

As it stands, the example code in the question does not seem to have any places where extra references could be "hiding". So if the memory usage goes up "every time" a tab is created (as the OP claims), then the problems must lie elsewhere in the application - or to be more precise, the problems probably lie in how the objects are created, rather than in how they are deleted.

这篇关于pyqt:内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆