PDA

View Full Version : QtWebKit with javascript: memory leak?



monkazer
19th June 2013, 20:55
Hi,

I've written a software which browses multiple websites containing javascript.
To achieve that, I'm using QWebPage with QWebSettings::JavascriptEnabled.
The problem is that over the time the ram usage is constantly increasing, until more than 1-2 gb of ram is used and then crashes.
I've written a little test snippet to demonstrate the issue.

Ram usage

Without running the snippet: 7,7mb
With QWebSettings::JavascriptEnabled set to false: 40mb
With QWebSettings::JavascriptEnabled set to true: 110mb


If you put some breakpoints, you can see the ram usage increasing with each site.

Do you have any solution so that I can reduce the amount of ram used? I guess the problem is the page/frame isn't deleted because some javascript code is executed (maybe?). If so, any way to force the javascript to stop, once the page is loaded? Or perhaps a way to force the page to delete all its content?




QWebPage page;
page.settings()->setAttribute(QWebSettings::JavascriptEnabled, true);

QEventLoop loop;
connect(&page, SIGNAL(loadFinished(bool)), &loop, SLOT(quit()));

for (int i=0; i<2; i++) {
page.mainFrame()->load(QUrl("http://www.youtube.com"));
loop.exec();

page.mainFrame()->load(QUrl("http://www.youtube.com"));
loop.exec();

page.mainFrame()->load(QUrl("http://www.9gag.com"));
loop.exec();

page.mainFrame()->load(QUrl("http://www.google.com"));
loop.exec();

page.mainFrame()->load(QUrl("http://www.facebook.com"));
loop.exec();

page.mainFrame()->load(QUrl("http://www.amazon.com"));
loop.exec();

page.mainFrame()->load(QUrl("http://www.twitter.com"));
loop.exec();

page.mainFrame()->load(QUrl("http://qt-project.org"));
loop.exec();

page.mainFrame()->load(QUrl("http://imdb.com"));
loop.exec();

page.mainFrame()->load(QUrl("http://techcrunch.com/"));
loop.exec();
}

I'm using Qt 5.1 RC

monkazer
20th June 2013, 20:02
Anyone? Please I really need to fix this issue...Is it a bug in Qt? The memory should be cleared once the QWebPage is destroyed.

monkazer
22nd June 2013, 13:39
Ok, I've found the issue. Sorry the code above wasn't totally accurate.
In my app I have QWebSettings::AutoLoadImages disabled. It looks like if QWebSettings::JavascriptEnabled is enabled, we also need to enable QWebSettings::AutoLoadImages, otherwise the memory usage is a lot higher.
I did the above test again without loading the images and the ram usage was 220mb!