Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any solution for the QtWebKit memory leak?

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_()
like image 989
Mikhail Gerasimov Avatar asked Jan 25 '14 22:01

Mikhail Gerasimov


1 Answers

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

like image 192
Mikhail Gerasimov Avatar answered Sep 20 '22 13:09

Mikhail Gerasimov