Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebBrowser control memory leak

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?

like image 592
Yozer Avatar asked Mar 21 '23 08:03

Yozer


2 Answers

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);
like image 200
CaptainBli Avatar answered Mar 31 '23 17:03

CaptainBli


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.

like image 25
gvisoc Avatar answered Mar 31 '23 19:03

gvisoc