PDA

View Full Version : Bad memory usage on QWebView I think



jiturra
17th February 2009, 19:32
Hi,

I'm new programming QT and c++.
I use QT 4.5 rc1 on Linux Debian.

Recently I discover the memory usage of my program is increasing after every web page visited.
After many hours looking for memory leaks, I make one simple application with only one QWebView and load one URL. Then after every loadFinished() signal, reload the same page over and over again.

Result: the memory usage report by "top" increasing rapidly and don't know why.

This is mainwindow.cpp



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(load()));

QWebPage *page = ui->webView->page();
QWebHistory *history = page->history();
history->setMaximumItemCount(0);

QWebSettings *settings = ui->webView->settings();
settings->setMaximumPagesInCache(0);
settings->setObjectCacheCapacities(0, 0, 0);
settings->setIconDatabasePath("");

settings->setAttribute(QWebSettings::JavascriptEnabled, true);
settings->setAttribute(QWebSettings::PluginsEnabled, true);
settings->setAttribute(QWebSettings::AutoLoadImages, true);
settings->setAttribute(QWebSettings::JavaEnabled, false);
settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
settings->setAttribute(QWebSettings::JavascriptCanOpenWindow s, true);
settings->setAttribute(QWebSettings::JavascriptCanAccessClip board, false);


ui->webView->setUrl(QUrl("http://<url>"));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::load()
{
ui->webView->setUrl(QUrl("http://<url>"));
}



I think qtwebkit isn't free memory or I'm do something bad, but don't know what can be.

Please, if you have any suggestions, help me.

(sorry for my not very good english)

Joel

wysota
17th February 2009, 23:52
You are not deleting the pages anywhere so they will be kept in memory until the QWebView object is destroyed. There is no memory leak, the deletion of objects is simply deferred. Either delete the pages when you load new ones or don't look at memory usage :)

jiturra
18th February 2009, 12:04
Hi,

What method you use for delete web page from memory ?

I understand you can re-use the same QWebView (or not?). My original application delete QWebView (inside QWidgetTab) and the memory problem is the same.

(sorry for de code tag)

thanks

Joel

talk2amulya
18th February 2009, 12:07
QWebPage is a QObject and deleteLater() is always a safe function to delete any object derived from QObject.

wysota
18th February 2009, 12:37
The fact that you don't see the memory usage decrease doesn't mean it's still allocated. The operating system may be keeping it assigned to your process "just in case" you want it back.

jiturra
18th February 2009, 14:00
ok,

I will try to see what happen with more execution time.

thanks...


Joel

jiturra
18th February 2009, 15:06
The fact that you don't see the memory usage decrease doesn't mean it's still allocated. The operating system may be keeping it assigned to your process "just in case" you want it back.

Hi,

this is memory usage (copied from "top" command on Linux) few minutes ago.


Before start application:


Mem: 2006812k total, 740224k used, 1266588k free, 19744k buffers
Swap: 1943824k total, 0k used, 1943824k free, 376932k cached



Just after start application:


Mem: 2006812k total, 908100k used, 1098712k free, 20780k buffers
Swap: 1943824k total, 0k used, 1943824k free, 419548k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7036 jiturra 20 0 135m 93m 19m S 77 4.8 0:21.85 tmp



After few minutes:


Mem: 2006812k total, 1784796k used, 222016k free, 21952k buffers
Swap: 1943824k total, 0k used, 1943824k free, 420444k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7036 jiturra 20 0 733m 691m 19m S 70 35.3 3:46.02 tmp


Mem: 2006812k total, 1957124k used, 49688k free, 776k buffers
Swap: 1943824k total, 305040k used, 1638784k free, 125080k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7036 jiturra 20 0 1270m 1.1g 19m R 77 57.1 7:08.45 tmp



After stop loading pages:


Mem: 2006812k total, 1956428k used, 50384k free, 1592k buffers
Swap: 1943824k total, 324028k used, 1619796k free, 127676k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7036 jiturra 20 0 1284m 1.1g 19m S 0 57.5 7:14.70 tmp



After stop application:


Mem: 2006812k total, 507500k used, 1499312k free, 2068k buffers
Swap: 1943824k total, 167056k used, 1776768k free, 132196k cached




As you can see, memory never was released, first consume all RAM and then consume SWAP. Only after kill application, the memory back to normal.
I still don't know if I'm doing something wrong. Don't look as OS memory management situation.

What I can do to test your theory ?

(sorry, I'm new on c++/QT)

Thanks,

Joel

wysota
18th February 2009, 17:07
Well... you'd have to show us the code where you delete pages and the webview object.

jiturra
18th February 2009, 18:02
Well... you'd have to show us the code where you delete pages and the webview object.

sorry, I'm a little confuse.

why you say I must delete pages and webview object ?

if I want to reuse the same object for many pages.

searching the code of QT I found in QWebFrame two methods,

setUrl(QUrl)
load(QUrl)

but don't found more information about differences

trying with load(), my test application is still increasing memory, but slowly.

please, tell me where I can found more documentation.

thanks.

wysota
18th February 2009, 18:59
why you say I must delete pages and webview object ?
As far as I remember it is you who said you are deleting them:


My original application delete QWebView (inside QWidgetTab) and the memory problem is the same.


if I want to reuse the same object for many pages.
You are reusing the webview object but not the webpage object. The page will probably get deleted once you delete the view but as you reuse the view, it's probably not getting deleted waiting until its parent is. You can call QWebView::back() and the previous page will get shown so it's probably not deleted because of that exact behaviour. If you want to get rid of it, then do it manually by calling delete (or deleteLater()) on the webpage object.

jiturra
18th February 2009, 19:28
As far as I remember it is you who said you are deleting them:

yes, but only in other application, not in this test program.




You are reusing the webview object but not the webpage object. The page will probably get deleted once you delete the view but as you reuse the view, it's probably not getting deleted waiting until its parent is. You can call QWebView::back() and the previous page will get shown so it's probably not deleted because of that exact behaviour. If you want to get rid of it, then do it manually by calling delete (or deleteLater()) on the webpage object.

the setUrl(QUrl) and load(QUrl) are method from QWebFrame object.



QWebView *web;
web->setUrl(QUrl("http://www.google.com"));

is equal to


QWebView *web;
web->page()->mainFrame()->setUrl(QUrl("http://www.google.com"));


QWebView use QWebPage and this use QWebFrame::setUrl method.

And I set no history, then no "back" and "forward" option are available.

One more comment. Using load(QUrl) method the memory increasing slowly (if setUrl consume 50% memory in 20 minutes, this method consume 25% memory in 1 hour)

Thanks

roland8454
22nd May 2009, 02:44
I have exactly the same problem Memory usage of QWebView go sky-rocket after a few page loads. Does anyone have a solution to this?

kerchen
22nd May 2009, 20:29
It would appear that this is a bug in QWebView:

https://bugs.webkit.org/show_bug.cgi?id=24458

roland8454
22nd May 2009, 20:47
It would appear that this is a bug in QWebView:

https://bugs.webkit.org/show_bug.cgi?id=24458

Hum, it may very well be. Let's see if the new version still have the same problem. thanks.

jpn
23rd May 2009, 17:52
There has been a relevant discussion on the qt4maemo-devel (https://garage.maemo.org/pipermail/qt4-devel/) mailing list recently with title "QWebKit memory usage with images on web page". Ariya Hidayat already applied some fixes (https://garage.maemo.org/pipermail/qt4-devel/2009-May/000494.html) to WebKit trunk.

buvintech
21st January 2014, 20:35
Read This For A "Pseudo-Solution" To The Qt Webkit Memory Leak:

http://qt-project.org/forums/viewthread/11105