How to Fix the Memory Leak in IE WebBrowser Control?

后端 未结 18 853
走了就别回头了
走了就别回头了 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 06:03

    It seems the Navigate() method keeps all the visited pages in memory as you can use the GoBack() method, there is no "memory leak" in fact. My program visits the same Url repeatedly. The "memory leak" problem can be eliminated by using the Refresh() method instand of Navigate() method, followed by a GC.Collect(). The Code is in the following:

           try
            {
                if (webBrowser.Url.Equals("about:blank")) //first visit
                {
                    webBrowser.Navigate(new Uri("http://url"));
                }
                else
                {
                    webBrowser.Refresh(WebBrowserRefreshOption.Completely);
                }
            }
            catch (System.UriFormatException)
            {
                return;
            }
            System.GC.Collect(); // may be omitted, Windows can do this automatically
    

提交回复
热议问题