How to Fix the Memory Leak in Ie Webbrowser Control

Memory Leak in IE WebBrowser Control

The solution is to call the garbage collector & force the Windows OS to clean the memory. For example into finally you can write :

finally
{
GC.Collect();
GC.WaitForPendingFinalizers();

EmptyWorkingSet(GetCurrentProcess());
}

EmptyWorkingSet forces the OS to clean the memory.

WebBrowser control memory leak

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);

webbrowser control with memory increasing problem

According to this answer to the same question, the memory increases at each page request/load, but will be released in a while - which means it is actually not a memory leak. Try to minimize the application window - is the memory released then?

In that case, you could try to trim the working set of the application periodically through this code (from this forum thread, the answer by mike_t2e):

// In class definition
[DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

[DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr GetCurrentProcess();

// ...

// Call this when you want to trim the working set
IntPtr pHandle = GetCurrentProcess();
SetProcessWorkingSetSize(pHandle, -1, -1);

Webbrowser Memory Leak

This code will free the memory similar to garbage collect. I found it useful to add it to a timer to automate the process.

    [DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

[DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr GetCurrentProcess();

private void button1_Click(object sender, EventArgs e)
{
IntPtr pHandle = GetCurrentProcess();
SetProcessWorkingSetSize(pHandle, -1, -1);
}

How to clear the WebBrowser from memory properly

You have removed the tab page from the tab control. You have not destroyed the tab page, nor have you destroyed the controls it contains.

In fact, immediately after the code shown, you could do

tabControl2.TabPages.Add(myTabPage);

to add that same tab page to a different tab control. Web browsers often do something similar, to enable the feature where you can drag-and-drop tabs to another window. It is also the way you temporarily make a tab page invisible, since there is no Visible property for the TabControl.

What you need to do is call the Dispose method for the tab page control, myTabPage. Normally, the framework would handle this automatically once its parent tab control is destroyed, but since you're manually removing the control from its parent container, that can't happen. You have to dispose of it manually. Modify your code to look like this:

tabControl1.TabPages.Remove(myTabPage);
myTabPage.Dispose();

This should dispose the resources of the tab page and its child controls, including the WebBrowser.

But that won't necessarily stop the sound, at least not immediately. To ensure that happens, you need to set the WebBrowser control's Source property to null. You must do this before disposing of the tab page (you can't modify properties on a disposed object).



Related Topics



Leave a reply



Submit