views:

2211

answers:

5

I am trying to embed a WebBrowser Control in a C# Winform Application. This sounds easy enough. However I discovered that the WebBrowser control eats up a lot of memory every time I call the Navigate method. The memory is never released. The memory usage grows and grows…

Many people on the net having the exact same problem but I haven’t found a satisfying answer yet. This is the best discussions about this issue I found so far:

Memory Leak in IE WebBrowser Control

One person suggested an upgrade to IE8 to fix the problem.

However I need a solution that works whether the user has the latest IE version installed or not. I do not have control over the users environment.

Does anybody know how to release the memory taken by the WebBrowser control? Are there workarounds? Are there alternatives to the WebBrowser control?

Update: I just did a few more tests. At work I am running Windows XP and IE6. The memory is not growing there. The memory increases when calling the navigate method but is being released after a while. At home I am running Vista and upgraded to IE8. Here I also do not see the problem anymore. It looks like the issue is specific to IE7. So the question should be rephrased to "How to Fix the Memory Leak in IE WebBrowser Control when IE7 is installed". Can anybody confirm that this problem is specific to IE7?

A: 

I'm using the Web Control in an application but since my application navigates to one page only I haven't noticed the issue you mentioned. There's another web control that is actually a wrapper and I don't know if it has the same problem or not. You can find it here.

Beatles1692
+2  A: 

There's an alternative control that uses Gecko (The engine Firefox uses) instead of Trident and works very well with the MSHTML interfaces.

Your pages will render in Gecko, and you'll have complete control over the settings, plugins, security and any other customisable features of a browser.

The downside is that you'll need to ship Gecko with your app, I last used the equivalent of Firefox 2 and it was around 8MB.

I released an app quite a while ago that compared IE and Firefox rendering alongside each other, both updating as you edited the CSS. I didn't run into the memory problems you've had with the web browser control, but I found the Gecko control very easy to work with. It doesn't have the same managed wrapper class that the .net WebBrowser control has, but it's easy enough to work around that.

Matthew Brindley
+1  A: 

I just created a simple app with a web browser control to try and duplicate your results. What I found was that yes, every time you navigate to a page, the memory being used increases significantly. HOWEVER, this is NOT a memory leak, because if you keep navigating, you'll see that after a short while, the memory drops significantly, indicating that the garbage collector did it's thing. To prove it, I forced the Garbage Collector to collect after every time I called Navigate, and the overall memory used stayed put at almost the same amount after every navigate call.

So while it DOES rack up memory every time you "Navigate" it's NOT a memory leak, and you the memory will be released. If it's raking up too quickly, just call GC.Collect();

BFree
I just did a few more tests. At work I am running Windows XP and IE6. The memory is not growing there. It's like BFree describes: The memory increases when calling the navigate method but is being released after a while. At home I am running Vista and upgraded to IE8. Here I also do not see the problem anymore. It looks like the issue is specific to IE7.
Rainer Falle
A: 

I was running into the same problem, as an alternative, instead of navigating to a new page, I simply rewrote the same html page using the system.oi.streamreader/writer object and calling a refresh. Obviously that won't work in a situation where content for the browser is being fed online, but it's doing the trick for me.

Also, I'm currently using 8+ browser controls all active at the same time to serve reporting through javascript inside my .net app. As a user makes one browser active, the html to which the other browsers are pointing, are cleared and the browsers refreshed. With 8 browsers running with these two methods I can easily keep my app well under the memory usage of Firefox with just 3 tabs open.

A: 

just use this to dispose it, works in win7, x64

      WebBrowser web;

      void RecreateBrowser()
      {
         if(web != null)
         {
            this.splitContainer1.Panel2.Controls.Remove(web);
            web.Dispose();
            web = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
         }
         web = new WebBrowser();

         web.AllowWebBrowserDrop = false;
         //web.IsWebBrowserContextMenuEnabled = false;
         web.WebBrowserShortcutsEnabled = false;
         web.ObjectForScripting = this;
         web.DocumentCompleted += new     WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted);

         this.web.Dock = System.Windows.Forms.DockStyle.Fill;
         this.web.Name = "web";
         this.splitContainer1.Panel2.Controls.Add(this.web);
      }
radioman