I have a QMainWindow object containing a button. It opens another QMainWindow containing a QTableView. When I close the mainwindow containing the table, most of the memory is released but not all of it. There seems like something is causing memory leak. I have tried destroying the objects, create close events which set the table to None, clear the table data etc. but nothing worked.
I am on windows and am just tracking memory usage with task manager. If i close and open the table window multiple times, the memory usage keeps increasing.
Memory usage:
Upon app start: 10.9 MB
After opening mainwindow containing table: 256.8 MB
After closing mainwindow containing table: 82.4 MB
What contains the 71.5 MB difference?
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QTableView, QMainWindow
from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QAction, QStandardItemModel, QStandardItem
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt6 App")
self.resize(200, 100)
# Button
self.button = QPushButton("Open Table")
self.button.clicked.connect(self.open_table)
# Layout
layout = QVBoxLayout()
widget = QWidget()
layout.addWidget(self.button)
widget.setLayout(layout)
self.setCentralWidget(widget)
def open_table(self):
# Open the table window
self.table_window = TableMainWindow()
self.table_window.show()
self.table_window.winClosed.connect(self.cleanup)
def cleanup(self):
self.table_window.table.clear_table() # does not seem to do anything
self.table_window.table.deleteLater() # does not seem to do anything
self.table_window.deleteLater()
class TableMainWindow(QMainWindow):
winClosed = pyqtSignal(object)
def __init__(self, parent=None):
super().__init__()
self.table = Table()
self.setup_ui()
def closeEvent(self, event):
self.winClosed.emit(self)
event.accept()
def setup_ui(self):
self.setGeometry(400, 50, 600, 500)
central_widget = QWidget()
main_layout = QVBoxLayout(central_widget)
main_layout.addWidget(self.table)
self.setCentralWidget(central_widget)
class Table(QTableView):
def __init__(self, parent=None):
super().__init__(parent)
self.columns = ["col1", "col2", "col3", "col4", "col5"]
data = [{col: "Sample Cell Text" for col in self.columns} for i in range(100000)]
self.build_table()
self.load_data(data)
def build_table(self):
self.model = QStandardItemModel(self)
self.setModel(self.model)
self.model.setColumnCount(len(self.columns))
self.model.setHorizontalHeaderLabels(self.columns)
def load_data(self, data):
for row in range(0, len(data)):
self.insert_row(row, data[row])
def insert_row(self, row, data):
items = []
for index, column in enumerate(self.columns):
items.append(QStandardItem(str(data[column])))
self.model.insertRow(row, items)
def clear_table(self):
self.model.removeRows(0, self.model.rowCount())
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
I have tried destroying the objects, create close events which set the table to None, clear the table data etc. but nothing worked. I was expecting for the memory to return to the level before I open the table mainwindow.