5

I have problem with memory leak in webBrowser control. I have found this thread:

How to get around the memory leak in the .NET Webbrowser control?

and this:

//dispose to clear most of the references
this.webbrowser.Dispose();
BindingOperations.ClearAllBindings(this.webbrowser);

//using reflection to remove one reference that was not removed with the dispose 
var field = typeof(System.Windows.Window).GetField("_swh", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

var valueSwh = field.GetValue(mainwindow);

var valueSourceWindow = valueSwh.GetType().GetField("_sourceWindow", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(valueSwh);

var valuekeyboardInput = valueSourceWindow.GetType().GetField("_keyboardInputSinkChildren", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(valueSourceWindow);

System.Collections.IList ilist = valuekeyboardInput as System.Collections.IList;

lock(ilist)
{
    for (int i = ilist.Count-1; i >= 0; i--)
    {
        var entry = ilist[i];
        var sinkObject = entry.GetType().GetField("_sink", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        if (object.ReferenceEquals(sinkObject.GetValue(entry), this.webbrowser.webBrowser))
        {
            ilist.Remove(entry);
        }
    }
} 

But I'm using Windows.Forms no WPF window and i have problem with converting this code to my needs. Can somebody help me?

Community
  • 1
  • 1
Yozer
  • 648
  • 1
  • 11
  • 26
  • Are you sure that WinForms has the leak at all? – SLaks Dec 11 '13 at 18:24
  • Yes. I'm monitoring physical memory that my app is using. And is growing and growing (not fast but still). My app is just one webbrowser navigating to big ammout of pages. – Yozer Dec 11 '13 at 18:28
  • What is the exact problem that prevents you from using this solution? – nestedloop Dec 16 '13 at 15:58
  • possible duplicate [How to get around the memory leak in the .NET Webbrowser control?](http://stackoverflow.com/questions/8302933/how-to-get-around-the-memory-leak-in-the-net-webbrowser-control). Read Sergey Kostrukov's answer. – Samy S.Rathore Dec 18 '13 at 12:27

3 Answers3

3

We faced that problem some time ago... to no avail.

To work around the problem and keep our application's memory consumption at a reasonable level we decided to split our application in two kind of processes, one for the main window and N child processes to host the WebBrowserControl. Then, design a pipe protocol (or RMI/RPC-like) to communicate events from the main window to the child processes and vice versa.

Doing that, you can design a recycle strategy using a pool of browser processes and a background kill-and-spawn policy to get the memory consumption at a controlled level.

gvisoc
  • 77
  • 3
3

We have used Chromium in a couple of applications. This allowed us to run HTML 5 in WinXP. Since the webBrowser control uses the installed IE of the OS you can't use most of the better HTML/Javascript. Microsoft doesn't support WinXP's IE so the application only can access older versions of IE.

If you use the CEFSharp version of Chromium you can even compile in further mods and aids for your navigation which gives you improved embedded communication that isn't supported by IE.

The code is really simple and there are several examples but just look:

InitializeComponent();
Text = "CefSharp";

web_view = new WebView("https://github.com/perlun/CefSharp", new BrowserSettings());
web_view.Dock = DockStyle.Fill;
toolStripContainer.ContentPanel.Controls.Add(web_view);
//even setup the console to log to a Textbox for debugging by setting up a Handler.
web_view.ConsoleMessage += new CefSharp.ConsoleMessageEventHandler(ConsoleMessageHandler);
CaptainBli
  • 4,121
  • 4
  • 39
  • 58
1

Have you considered using other web browser controls? There are a lot out there! I feel like chromium is a good bet, check out this question for alternatives.

Community
  • 1
  • 1
Aleksei Poliakov
  • 1,322
  • 1
  • 14
  • 27