C# Vistual Studio 2010 WebBrowser Control Memory Issue

Answered C# Vistual Studio 2010 WebBrowser Control Memory Issue

  • Sunday, August 05, 2012 11:49 PM
     
     

    Hi guys, this is my first question here at MSDN and I'm grateful to anyone who is willing to help me.

    I'm having problems with my C# Forms application, I think it could be a memory leak.

    My application navigates through a lot of pages and it uses more and more memory, until it reaches a point where it throws an exception telling me all my resources are used up and I'm out of memory.

    I'm using the WebBrowser control for navigation and I have already tried the following without success:

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

    Got that from this thread, but it didn't change a thing:

    http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/88c21427-e765-46e8-833d-6021ef79e0c8/

    2. I also tried forcing garbage collection, again without success.

    Now, I'm stuck and I need help, thank you for your time.


    Extra Info:

    I have IE9 installed, 6GB of RAM and Vistual Studio 2010.

    • Edited by Vreg Sunday, August 05, 2012 11:50 PM
    •  

All Replies

  • Monday, August 06, 2012 12:25 AM
     
     
    What is the exact error message?
     
    It sounds like you are not disposing of objects somewhere in your app. Calls to SetProcessWorkingSetSize would not likely help with that problem.

    --
    Mike
  • Monday, August 06, 2012 3:46 AM
     
     

    This is the error I get:

    System.Runtime.InteropServices.COMException was unhandled by user code
    The requested resource is in use. (Exception from HRESULT: 0x800700AA)

  • Monday, August 06, 2012 10:16 PM
     
     
    If you are using the browser control to automate some processes, you might be handling the document completed event and holding onto some com objects within the handler.  It is virtually impossible to help without knowing the architecture of your app.

    --
    Mike
  • Tuesday, August 07, 2012 6:09 PM
     
     

    Well I just rebooted the program and here is the exact error:

    System.Runtime.InteropServices.COMException was unhandled
      Message=The requested resource is in use. (Exception from HRESULT: 0x800700AA)
      Source=System.Windows.Forms
      ErrorCode=-2147024726
      StackTrace:
           at System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
           at System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
           at System.Windows.Forms.WebBrowser.Navigate(String urlString)
           at Application_X.mainForm.infoOpener_Tick(Object sender, EventArgs e) in D:\Projects\Application_X\Application_X\mainForm.cs:line 97
           at System.Windows.Forms.Timer.OnTick(EventArgs e)
           at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
      InnerException: 

    I'm not handling the document completed event because it does not work well with complex sites, in order to have a delay between navigation's I use a Timer. Anyway, I can understand why this exception is thrown because literally, every navigation to a site takes up 10 MB of RAM, these sites are however complex with a lot of scripts etc. 

    I find it very irrational why the memory is kept occupied after navigating to a site. I need a way for the webbrowser control to release memory. Thank you for your time and attention.

  • Tuesday, August 07, 2012 11:24 PM
     
     
    Ok, so you are not handling the navigate completed event, but you are hoping the page has completed navigation at some point to interact with the page, correct?  If so, what is it you are doing when you interact with the page?
     
    I would suspect you are creating some objects to work with the page, but the next timer tick is taking you away from the page, leaving objects undisposed. 

    --
    Mike
  • Saturday, August 11, 2012 1:22 AM
     
     

    Today I did some debugging and I pinpointed the leak, my managed memory is as it should be.

    The leak is located at the navigation of the webbrowser control. I loaded a bunch of URL's in an array, then I dumped the memory, everything was perfect, but when I started JUST navigating each one of them every 6 seconds, the application started using about 25 MB unmanaged memory for every site the control navigated to.

    At least I know it's not my application's design but Microsoft's libraries.

    So now the question is, how do I solve this? I need to do this with the webbrowser control, because my HTML parser is designed to work with it.

  • Saturday, August 11, 2012 6:33 AM
     
     Proposed Answer

    WebBrowser control if you are using, you don't need to use win32 API's.

    in the function try to create WebBrowser control for each navigation as shown below:

    using(WebBrowser browser = new WebBrowser(url))

    {

    }

    Call GC.Collect() for instant memory release

     also call GC.WaitForPendingFinalizer().

    In this way you can easily control memory consumption

    • Proposed As Answer by kishhr Saturday, August 11, 2012 6:45 PM
    •  
  • Saturday, August 11, 2012 5:16 PM
     
     

    That does not do the trick for me, 2 reasons why:

    1. If you want to navigate to a page and parse it's HTML, there's no delay between those two steps.

    2. For some reason, I cannot pass the value of the WebBrowser control's HTMLDocument to a HTMLDocument property in my application (this needs to be done in my application if I'm going to destroy the control after every navigation). The cause of this might be reason 1.

    So I'm still stuck with this stupid problem and I really need this fixed, anyone any other ideas?

    Thanks.


    • Edited by Vreg Saturday, August 11, 2012 5:19 PM
    •  
  • Monday, August 13, 2012 9:47 PM
     
     Answered
    Problem solved, just gave up on Microsoft's shitty Trident engine and completely replaced it with Mozilla's Gecko engine. Works like a charm and like really, my RAM stays idle.
    • Marked As Answer by Vreg Monday, August 13, 2012 9:49 PM
    •