How can C# WebBrowser reduce the memory occupying/How to solve WebBrowser's memory leak?

纵饮孤独 提交于 2021-02-11 17:00:23

问题


I'm using WebBrowser to write a program (let's call it < tinyWb >) to do some automatic web form filling jobs, etc., just for fun. < tinyWb > mainly does the following jobs: open a tabPage, navagate to a URL, do some business, and then close the tab.

There are about 25 URLs total that need to be navigated to, but I found that the memory that the < tinyWb > program is occupying is growing continuously, and after about 10 hours it will require about 120m physical memory and about 240m virtual memory, though it only requires about 40m physical memory at the first hour.

Using GC is not a useful solution for this issue apparently, because it seems like some objects are never being disposed, because I found that when I called GC, it only makes the physical memory occupying convert to virtual memory occupying, the total memory occupying is never reduced.

I had written some code to try to dispose them when tabWindows is closed like this:

// Find the active page
TabPage page = this._tabControl.SelectedTab;
// Check whether there is actually a page selected
if (page != null)
{
    //<try to solve the memory leak -1- start>
    ControlCollection controls = page.Controls;
    if (controls!=null) {
        foreach (Control control in controls) {
            if (control != null)
            {
                control.Dispose();
            }
        }
    }
    //<try to solve the memory leak -1- end>
    // Remove the page
    this._tabControl.TabPages.Remove(page);
    //<try to solve the memory leak -2- start>
    page.Dispose();
    //<try to solve the memory leak -2- end>
}

The code above is following the solution of JDBC memory leak, but I'm not so sure, are there other objects that need to be disposed except controls and page objects?

I had to do lots of searching on the internet,

  • some threads said to call some Windows APIs, but it was similar to the GC solution;
  • some threads said to upgrade IE to IE8, but I had already upgraded to IE11;
  • some threads said it's a bug of tabControl, and IE has the same problem, so I had done a test on three different PCs like this:

open a new tab, navigate urlA, after document was loaded, close the tab, then open a new tab again, navigate urlB, after document was loaded, close the tab, and so on... after about five times testing I found that the memory occupied by IE is growing, just like < tinyWb >.

I also had done a similar test like this:

open a new tab, navigate to urlA, after document was loaded, navigate to urlB, after document was loaded, navigate to urlA, and so on...(never close the tabPage). After about five times testing it still had the same problem of memory growing.

So, if it's really a bug of IE tabControl, is there any hotfix or tricks or manual patches, even some cracks to solve the bug? Or any other ways?

Many many thanks.

来源:https://stackoverflow.com/questions/65624435/how-can-c-sharp-webbrowser-reduce-the-memory-occupying-how-to-solve-webbrowsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!