Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带有样式表的pyqt内存泄漏_Python_Qt_Qt4_Pyqt_Pyqt4 - Fatal编程技术网

Python 带有样式表的pyqt内存泄漏

Python 带有样式表的pyqt内存泄漏,python,qt,qt4,pyqt,pyqt4,Python,Qt,Qt4,Pyqt,Pyqt4,pyqt4 | QT4.8 | linux(开放suse)| python 2.7 我有一个关于pyqt和python内存的奇怪行为的简单问题 我做了一个小测试,一个小部件,在这个小部件上创建了1000个按钮,并将小部件的父项设置为“无” 我认为好的行为是,在widget(里面有1000个按钮)上,如果我们使用widget.setParent(None),qt将破坏它(和他的所有孩子) 但是,如果我们添加一个widget.setStyleSheet('background-color:green

pyqt4 | QT4.8 | linux(开放suse)| python 2.7

我有一个关于pyqt和python内存的奇怪行为的简单问题

我做了一个小测试,一个小部件,在这个小部件上创建了1000个按钮,并将小部件的父项设置为“无”

我认为好的行为是,在widget(里面有1000个按钮)上,如果我们使用widget.setParent(None),qt将破坏它(和他的所有孩子)

但是,如果我们添加一个widget.setStyleSheet('background-color:green'),似乎qt不会破坏按钮,或者python不会释放这些按钮的内存

一个简单的测试代码。(良好行为)

现在,与样式表的示例相同

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        widgetCentral = QtGui.QWidget(self)
        widgetCentral.setStyleSheet('background-color:green;')
        for dummy in range(1000):
            beginBtn = QtGui.QPushButton(widgetCentral)
            beginBtn.setText('btn')

        # with or without this line, the memory is 22mo/23mo
        #widgetCentral.setParent(None) 

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
因此,使用样式表和python内存(垃圾?)

有人知道这种行为吗?
这是一个bug还是一个糟糕的实现

Python可以尝试将内存返回到系统,但无法保证操作系统能够释放内存(因为内存碎片)。您是否尝试过使用较新版本的python进行测试?我认为python3可能比python2处理得更好。我无法在Windows8.1、python2.7(32位)、PyQT4V4.11.2(二进制安装程序)上复制。在第二个示例中,取消注释
widgetCentral.setParent(None)
似乎可以正确地释放内存。我注意到的唯一一件事是,对于所有示例,我的内存使用量都比你的小得多(因此我将按钮的数量增加到10000,以便内存使用更明显)
class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        widgetCentral = QtGui.QWidget(self)
        widgetCentral.setStyleSheet('background-color:green;')
        for dummy in range(1000):
            beginBtn = QtGui.QPushButton(widgetCentral)
            beginBtn.setText('btn')

        # with or without this line, the memory is 22mo/23mo
        #widgetCentral.setParent(None) 

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())