Question

Memory size of QtWebKit process increases with every new page load. Cleaning memory cache doesn't help. Does anyone know how to solve it?

This simple example crashes after some time of operation:

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebKitWidgets import QWebView
from PyQt5.QtWebKit import QWebSettings

class Crawler(QWebView):
    def __init__(self):
        QWebView.__init__(self)

        self.settings().setMaximumPagesInCache(0)
        self.settings().setObjectCacheCapacities(0, 0, 0)
        self.settings().setOfflineStorageDefaultQuota(0)
        self.settings().setOfflineWebApplicationCacheQuota(0)
        self.settings().setAttribute(QWebSettings.AutoLoadImages, False)

        self.loadFinished.connect(self._result_available)

    def start(self):
        self.load(QUrl('http://stackoverflow.com/'))

    def _result_available(self, ok):
        print('got it!')

        self.settings().clearMemoryCaches() # it doesn't help
        self.settings().clearIconDatabase()

        self.start() # next try

if __name__ == '__main__':
    app = QApplication([])
    crawler = Crawler()
    crawler.start()
    app.exec_()
Was it helpful?

Solution

Reason of memory leak in disabled autoloading of images. It's a bug that will be fixed in next QT version. Removing this line solves the problem for example above:

self.settings().setAttribute(QWebSettings.AutoLoadImages, False)

Second possible reason which can lead to leaks is "Memory leak in GStreamer". It's in process.

Update:

I see people still looking for a solution. I've recently noticed bug with AutoLoadImages=False was not fixed in version Qt 5.2.1, nor in Qt 5.3 RC. New discussion about it has been opened. You can vote for this issue in bugtracker to increase the chances for fix in Qt 5.3.0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top