Is there any solution for the QtWebKit memory leak?
Asked Answered
D

1

9

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_()
Dorey answered 25/1, 2014 at 22:37 Comment(1)
Not really. It appears to be a somewhat baffling "feature" of webkit (see here for some discussion). One rather ugly workaround is to periodically start a new process so that the system can reclaim the memory.Newbold
D
6

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

Dorey answered 8/2, 2014 at 6:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.