1 year ago

#379426

test-img

KotoWhiskas

QThreadPool doesn't release memory after QRunnable has ended

I'm trying to implement image thumbnail loading. I use QThreadPool to make it multicore, but when an image loads, it doesn't free memory.

This problem occurs even if I don't use QImage object afterwards. Neither deleting variables and qrunnable nor using gc.collect or adding "self." to all object declarations worked. And this memory can't be reused after a new object is created, so it can crash with out of memory. Multiple QImage objects can take up to 1gb RAM.

from PyQt5.QtCore import QRunnable, QThreadPool, QThread, QDirIterator, QDir
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QImage
import logging, time
logging.basicConfig(format="%(message)s", level=logging.INFO)


class LoadPreviewIcon(QRunnable):
    def __init__(self, imagePath):
        super(LoadPreviewIcon, self).__init__()
        self.imagePath = imagePath

    def run(self):
        QImage(self.imagePath)


if __name__ == '__main__':
    app = QApplication([])
    pool = QThreadPool()
    pool.setMaxThreadCount(QThread.idealThreadCount())
    pathToFolderWithImages = '/path/to/folder/with/images'
    iterator = QDirIterator(pathToFolderWithImages,
                            QDir.Files | QDir.NoDotAndDotDot,
                                flags=QDirIterator.Subdirectories)
    while iterator.hasNext():
        img = iterator.next()
        pool.start(LoadPreviewIcon(img))
    time.sleep(20)

python

qt

pyqt

qthread

qrunnable

0 Answers

Your Answer

Accepted video resources