How to Fix the Memory Leak in IE WebBrowser Control?

后端 未结 18 718
走了就别回头了
走了就别回头了 2020-11-27 05:33

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 ever

相关标签:
18条回答
  • 2020-11-27 05:42

    my app was also constantly consuming memory when navigating, and not releasing anymore. i fount the solution for me here: http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/88c21427-e765-46e8-833d-6021ef79e0c8

    for completeness ill post the notable excerpt:

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

    -- code to call when you want to reduce the memory

            IntPtr pHandle = GetCurrentProcess();
            SetProcessWorkingSetSize(pHandle, -1, -1);
    

    all honors to: http://social.msdn.microsoft.com/profile/mike_t2e/?type=forum&referrer=http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/88c21427-e765-46e8-833d-6021ef79e0c8 for posting the solution.

    and http://ict-engineer.blogspot.com/2010/10/net-webbrowser-control-memory-leak.html for SEO'ing it right, so i could find it ;)

    greetings

    edit: if this helps you to quickly solve an issu - good. but you should overthing your application design, the pattern you use if any , refactore the thing if you build onto that much longer ....

    0 讨论(0)
  • 2020-11-27 05:43

    It's almost end of 2017 and still this annoying bug is present in WebBrowser.

    I've tried all solutions here and none of them worked for me. Memory Leak still persists... The shtrangest thing is that when I call:

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    
    IntPtr pHandle = GetCurrentProcess();
    SetProcessWorkingSetSize(pHandle, -1, -1);
    

    It does actually reduce the memory alot! but when the next Navigate instruction is called all the leaked memory comes back in scope.... like (if memory is at 450mb.. this instructions reduces to about 20 mb and right after calling .Navigate(string) again it jumps to 460mb and memory leak goes on...

    I even tried Dispose(), navigating to about:blank before the next page, setting webbrowser object to null and creating a new one. All these attempts fall in memory leak... this is really frustrating... Any other solutions?

    0 讨论(0)
  • 2020-11-27 05:43

    Paste the following code after page load

    System.Diagnostics.Process loProcess = System.Diagnostics.Process.GetCurrentProcess();
    try
    {
         loProcess.MaxWorkingSet = (IntPtr)((int)loProcess.MaxWorkingSet - 1);
         loProcess.MinWorkingSet = (IntPtr)((int)loProcess.MinWorkingSet - 1);
    }
    catch (System.Exception)
    {
         loProcess.MaxWorkingSet = (IntPtr)((int)1413120);
         loProcess.MinWorkingSet = (IntPtr)((int)204800);
    }
    
    0 讨论(0)
  • 2020-11-27 05:44

    I think this question has gone unanswered for a long time now. So many threads with the same question but not conclusive answer.

    I have found a work around for this issue and wanted to share with you all who are still facing this issue.

    step1: Create a new form say form2 and add a web-browser control on it. step2: In the form1 where you have your webbrowser control, just remove it. step3: Now, go to Form2 and make the access modifier for this webbrowser control to be public so that it can be accessed in Form1 step4: Create a panel in form1 and create object of form2 and add this into panel. Form2 frm = new Form2 (); frm.TopLevel = false; frm.Show(); panel1.Controls.Add(frm); step5: Call the below code at regular intervals frm.Controls.Remove(frm.webBrowser1); frm.Dispose();

    Thats it. Now when you run it, you can see that webbrowser control loaded and it will get disposed at regular intervals and there is no more hanging of the application.

    You can add the below code to make it more efficient.

        IntPtr pHandle = GetCurrentProcess();
        SetProcessWorkingSetSize(pHandle, -1, -1);
    
    
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    
    0 讨论(0)
  • 2020-11-27 05:53

    The BASIC IDEA is,

    "Kill myself, and reborn."

    Windows will solve all memory problems.

    but if you Close your application first, you can't start a new one.

    So, START A NEW ONE, and CLOSE THE OLDER ONE.

    First turn on a new one, and turn off an old one.


    public void SOLVE_ALL_MY_MEMORY_PROBLEM()
    {
      System.Diagnostics.Process.Start("MyProgram.exe");
      Application.Exit();
    }
    

    https://www.youtube.com/watch?v=aTBlKRzNf74

    If there is a parameter,

    public void SOLVE_ALL_MY_MEMORY_PROBLEM()
    {
      System.Diagnostics.Process.Start("MyProgram.exe", "PARA_para_dance");
      Application.Exit();
    }
    

    Go to Program.cs

        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if(args.Count() > 0)
                Application.Run(new Form1(args[0]));
            else
                Application.Run(new Form1());
        }
    

    and, Go to Form1.cs and make another Form1()

        public Form1()
        {
            InitializeComponent();
        }
    
        public Form1(string dance_name)
        {
            InitializeComponent();
    
            ...
        }
    

    or you can use temp file !!!

    0 讨论(0)
  • 2020-11-27 05:53

    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.

    0 讨论(0)
提交回复
热议问题