jimu 发表于 2016-11-28 18:31:27

关于QWebView内存

本帖最后由 jimu 于 2016-11-28 18:33 编辑

http://bbs.csdn.net/topics/390261419QWebView* webView=new QWebView(this); //创建QWebView
   webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
    webView->load(QUrl(newsUrl));之后、不管你是delete还是怎么地、指针可以删除、

但是总有那么14M左右的内存怎么都释放不了


求思路 、 求思路 、求思路



stackoverflow 是这样回答的
when you call delete the memory isn't freed in that moment it takes a while and if other objects are not created it might not geed freed until the end of the application. QWebView is a complex class 20 M in 2012 are no longer a concern.The memory will be freed if you do this properly

jimu 发表于 2016-11-28 18:33:06

#include <QtGui/QApplication>
#include <QWebView>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebView* w=new QWebView();
    w->settings()->setObjectCacheCapacities(0,0,0);
    w->load(QUrl("http://www.sina.com.cn"));
   w->show();
   delete w;
   return a.exec();
}木有QWebView运行内存是5M、加上QWebView内存是17M

for(int i=0;i<1000;i++){
QWebView* w=new QWebView();
    w->settings()->setObjectCacheCapacities(0,0,0);
    w->load(QUrl("http://www.sina.com.cn"));
   w->show();
   delete w;
}

这样运行之后、内存还是一样的!
我个人认为,QWebview加载了内容,就是已经放入内存了。就算你delete了,那部分内存还是被分配给你了。这时只能等系统回收了。而且这种情况,有点类似于以前的非Qt程序也会有这样的情况。你试试最小化后再看看内存占用数?


应该就是只有等自动回收、或者程序退出!


如果我程序一直不退出来,还是不是内存一直会增大?无法释放,到最后是不是系统崩溃???

jimu 发表于 2016-11-29 16:46:45

本帖最后由 jimu 于 2016-11-29 16:52 编辑

http://www.qtcentre.org/threads/ ... on-QWebView-I-think

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.
And I set no history, then no "back" and "forward" option are available.




It would appear that this is a bug in QWebView:

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

jimu 发表于 2016-11-29 17:09:05

http://stackoverflow.com/questions/19282255/qwebview-on-qt4-and-qt5

jimu 发表于 2016-11-29 17:09:29

http://www.qtcentre.org/threads/18853-Bad-memory-usage-on-QWebView-I-think?goto=nextnewest

jimu 发表于 2016-11-29 17:30:50

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


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

jimu 发表于 2016-11-29 17:35:18

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

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

jimu 发表于 2016-11-29 17:52:54

QWebElement body=myWebView->page()->mainFrame()->findFirstElement("body");body.setAttribute("onunload","myFunction()");
QWebSettings::clearMemoryCaches();webView->history()->clear();
页: [1]
查看完整版本: 关于QWebView内存