Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DimaSavostianov/dd92a3eb7bf7b499ddd60b246532fcf4 to your computer and use it in GitHub Desktop.
Save DimaSavostianov/dd92a3eb7bf7b499ddd60b246532fcf4 to your computer and use it in GitHub Desktop.
c# webbrowser memory leak SOLUTION
public partial class Form1 : Form
{
[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 ReNewBrowser()
{
webBrowser1.Dispose();
webBrowser1 = null;
IntPtr pHandle = GetCurrentProcess();
SetProcessWorkingSetSize(pHandle, -1, -1);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
webBrowser1 = new WebBrowser();
//Это уже мои заморочки, но на всякий случай не забудьте про них
webBrowser1.Location = new Point(11, 42);
webBrowser1.Size = new Size(1054, 659);
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
webBrowser1.Navigating += webBrowser1_Navigating;
splitContainer1.Panel1.Controls.Add(webBrowser1);
// конец заморочек
webBrowser1.Show();
}
}
@jocgoran
Copy link

Thank you! Solve a lot

@akontanistov
Copy link

Working. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment