locked
WebBrowser memory leak - is there any real solution? RRS feed

  • Question

  • Around the internet there are plenty questions regarding WebBrowser memory leak problem. 

    This is how you can confirm the problem: start a new project and add WebBrowser and Timer (shorter delay, will produce faster effect I set mine to 100 ms).

    Public Class Form1
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        WebBrowser1.Navigate("www.microsoft.com")
      End Sub
    
    End Class
    

    And beyond the obvious part, which is that page won't load due to insufficient time - you should see a nice steady memory leak -  around 8-50 kB for each WebBrowser1.Navigate().

    • Happens on every page I tried it on, even about:blank, seems to depend on website size,
    • Changing browser version has no impact (7, 8, 9),
    • Trying to dispose WebBrowser1 and addind it again to the form won't work,
    • Trying to dispose Form1 and recreating it won't work,
    • Playing with GC won't work,
    • Minimizing and setting process available memory to -1, -1 only reduces physical memory usage, has no impact on virtual memory,
    • Disabling various objects - flash, javascript, images - won't work (no surprise when leak occurs even with about:blank),
    • leak can be measured using all the tools imaginable (I have seen suggestions to use advanced tools over Task Manager - trust me I don't need it when my program crashes after few hours after growing to 1,5 GB of memory)
    • So far the only way to free this memory is to close application and run it again

    So, I am sure most of you reading this are well aware of this problem. I mean, there are so many posts about it, it must be either very common or at least well known to community. My question is: can I force WebBrowser to act civilized and prevent memory leak?

    Downloading webpage source will not do, I *need* WebBrowser to work - I interact with JavaScript on the website.

    Yours frustratedly

    Marcin Stefański

    Friday, March 18, 2011 11:03 PM

Answers

  • You can do this by simply using a separated AppDomain to load and display your form/webBrowser.

    You can comunicate between different appDomains and recover the data you need in your main appDomain.

    When you Unload the secondary AppDomain, the memory is cleared correctly.

    Good Luck.


    ...
    • Marked as answer by Paul Zhou Monday, March 28, 2011 2:12 AM
    Saturday, March 19, 2011 4:49 PM

All replies

  • You can do this by simply using a separated AppDomain to load and display your form/webBrowser.

    You can comunicate between different appDomains and recover the data you need in your main appDomain.

    When you Unload the secondary AppDomain, the memory is cleared correctly.

    Good Luck.


    ...
    • Marked as answer by Paul Zhou Monday, March 28, 2011 2:12 AM
    Saturday, March 19, 2011 4:49 PM
  • Looks like a good question for the IE extension development forum.

    Javascript circular reference leaking is nothing new (and I don't know how to force the js engine to GC even if you can break the circular references, you may want to ask that in the IE web development forum). And a travel log of the current page is supposed to be preserved in case you want to go back (online payment web site developers hate this because they have to block duplicate submissions). The only reliable way I ffound is to spin-off child processes for each session and close the processes as soon as you are done (kinda like what the LCIE featuer does in IE8)



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Saturday, March 19, 2011 8:27 PM
  • I use Process.Start("external.exe") that launches an external exe with a reference to my app.

    The exe calls a method in my app that does the browsing then exits, so no memory leaks in the original app, only in the second instance called by the external exe.

    In my real project I have this:

    Dim WithEvents Web as WebBrowser
    Sub NavigateManyPages()
    Proc = Process.Start("external.exe")
    Proc.WaitForExit
    End Sub
    Sub CallFromOtherExe()
    Web.Navigate(...
    End Sub

    In the external exe I have:

    Imports MyApp
    Sub Form_Load
    CallFromOtherExe()
    Me.Close
    End Sub


    Tuesday, August 27, 2013 9:52 PM