При каждом вызове дочернего окна, программа потребляет все больше памяти

Допустим, у меня есть код (с главным и дочерним окном):

import sys
from PyQt5.QtWidgets import  QApplication, QMainWindow, QPushButton


class ChildWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)  
        self.setWindowTitle("child window")
        self.resize(400, 100)


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle("main window")
        self.resize(400, 100)
         
        self.button = QPushButton("child window", self)
        self.button.setGeometry(0,0, 200, 50)
        self.button.clicked.connect(self.createChildWindow)

    def createChildWindow(self):
        self.window = ChildWindow(self)
        self.window.show()


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

Если пользователь вызовет дочернее окно и закроет, то количество потребляемой памяти программой увеличиться, несмотря на закрытие.
Получается с каждым вызовом количество потребляемой памяти будет увеличиваться.

Потребление памяти

(При запуске программа потребляла 10 МБ, после нескольких вызовов дочернего окна 15,2 МБ)

Почему дочернее окно ест память, даже если его закрыть?
Как это исправить?


Ответы (2 шт):

Автор решения: wchistow

При закрытии окна объект, который вы создали:

self.window = ChildWindow(self)

не удаляется из памяти.

→ Ссылка
Автор решения: S. Nick

Попробуйте так:

import sys
from PyQt5.QtWidgets import  QApplication, QMainWindow, QPushButton


class ChildWindow(QMainWindow):
    def __init__(self, parent=None):
        super(ChildWindow, self).__init__(parent)  
        self.setWindowTitle("child window")
        self.resize(400, 100)
    

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("main window")
         
        self.button = QPushButton("child window", self)
        self.button.setGeometry(0, 0, 200, 50)
        self.button.clicked.connect(self.createChildWindow)
        
        self.window = ChildWindow(self)                           # +++

    def createChildWindow(self):
#        self.window = ChildWindow(self)                          # ---
        self.window.show()
        

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.resize(500, 500)
    window.show()
    sys.exit(app.exec_())

введите сюда описание изображения

→ Ссылка