1

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)
  • I cannot understand Qt in Python but maybe after that image was used try `img.deleteLater()`. – Alexander V Apr 05 '22 at 21:02
  • @AlexanderV Nope, I do not think this will help. `img` is the path (`QString`) to the image, not the image itself. – HiFile.app - best file manager Apr 05 '22 at 21:27
  • I'm not able to reproduce this on Linux even with a 1.2gb directory of images, and it never goes beyond 150-200mb of usage. – musicamante Apr 05 '22 at 23:14
  • @musicamante i'm also on linux. The problem isn't even in memory usage. Before the program terminates (while it's on time.sleep), this memory is still used regardless of fact that QRunnable has ended, and it can't be reused for new qrunnables. Also, memory usage depends on image size – KotoWhiskas Apr 06 '22 at 09:05

0 Answers0