Results 1 to 16 of 16

Thread: Bad memory usage on QWebView I think

  1. #1
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Bad memory usage on QWebView I think

    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


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    6. {
    7. ui->setupUi(this);
    8. connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(load()));
    9.  
    10. QWebPage *page = ui->webView->page();
    11. QWebHistory *history = page->history();
    12. history->setMaximumItemCount(0);
    13.  
    14. QWebSettings *settings = ui->webView->settings();
    15. settings->setMaximumPagesInCache(0);
    16. settings->setObjectCacheCapacities(0, 0, 0);
    17. settings->setIconDatabasePath("");
    18.  
    19. settings->setAttribute(QWebSettings::JavascriptEnabled, true);
    20. settings->setAttribute(QWebSettings::PluginsEnabled, true);
    21. settings->setAttribute(QWebSettings::AutoLoadImages, true);
    22. settings->setAttribute(QWebSettings::JavaEnabled, false);
    23. settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
    24. settings->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
    25. settings->setAttribute(QWebSettings::JavascriptCanAccessClipboard, false);
    26.  
    27.  
    28. ui->webView->setUrl(QUrl("http://<url>"));
    29. }
    30.  
    31. MainWindow::~MainWindow()
    32. {
    33. delete ui;
    34. }
    35.  
    36. void MainWindow::load()
    37. {
    38. ui->webView->setUrl(QUrl("http://<url>"));
    39. }
    To copy to clipboard, switch view to plain text mode 



    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
    Last edited by wysota; 18th February 2009 at 00:52. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Bad memory usage on QWebView I think

    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

  3. #3
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Bad memory usage on QWebView I think

    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

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Bad memory usage on QWebView I think

    QWebPage is a QObject and deleteLater() is always a safe function to delete any object derived from QObject.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Bad memory usage on QWebView I think

    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.

  6. #6
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Bad memory usage on QWebView I think

    ok,

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

    thanks...


    Joel

  7. #7
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Bad memory usage on QWebView I think

    Quote Originally Posted by wysota View Post
    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Bad memory usage on QWebView I think

    Well... you'd have to show us the code where you delete pages and the webview object.

  9. #9
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Bad memory usage on QWebView I think

    Quote Originally Posted by wysota View Post
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Bad memory usage on QWebView I think

    Quote Originally Posted by jiturra View Post
    why you say I must delete pages and webview object ?
    As far as I remember it is you who said you are deleting them:

    Quote Originally Posted by jiturra
    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.

  11. #11
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Bad memory usage on QWebView I think

    Quote Originally Posted by wysota View Post
    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.


    Quote Originally Posted by wysota View Post
    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.

    Qt Code:
    1. QWebView *web;
    2. web->setUrl(QUrl("http://www.google.com"));
    To copy to clipboard, switch view to plain text mode 
    is equal to
    Qt Code:
    1. QWebView *web;
    2. web->page()->mainFrame()->setUrl(QUrl("http://www.google.com"));
    To copy to clipboard, switch view to plain text mode 

    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

  12. #12
    Join Date
    Feb 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Bad memory usage on QWebView I think

    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?
    Last edited by roland8454; 22nd May 2009 at 07:20.

  13. #13
    Join Date
    Feb 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Bad memory usage on QWebView I think

    It would appear that this is a bug in QWebView:

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

  14. #14
    Join Date
    Feb 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Bad memory usage on QWebView I think

    Quote Originally Posted by kerchen View Post
    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.

  15. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Bad memory usage on QWebView I think

    There has been a relevant discussion on the qt4maemo-devel mailing list recently with title "QWebKit memory usage with images on web page". Ariya Hidayat already applied some fixes to WebKit trunk.
    J-P Nurmi

  16. #16
    Join Date
    Jan 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Bad memory usage on QWebView I think

    Read This For A "Pseudo-Solution" To The Qt Webkit Memory Leak:

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

Similar Threads

  1. Program memory usage in windows
    By gerix in forum Installation and Deployment
    Replies: 4
    Last Post: 2nd February 2009, 01:35
  2. CPU Time & Memory Usage in QThread or QProcess
    By Davidaino in forum Qt Programming
    Replies: 0
    Last Post: 11th July 2008, 20:15
  3. CPU and Memory Usage
    By philwinder in forum Qt Programming
    Replies: 16
    Last Post: 17th May 2008, 23:25
  4. Memory usage in QTable
    By mahiapkum in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2007, 22:22
  5. How to determine memory usage?
    By nopalot in forum General Programming
    Replies: 3
    Last Post: 11th February 2007, 20:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.