Results 1 to 19 of 19

Thread: WebBrowser memory leak - is there any real solution?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    46

    WebBrowser memory leak - is there any real solution?

    I posted the same question on http://social.msdn.microsoft.com, but they seem to be less active - so I hope maybe you can help me out.

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

    Code:
    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. I also *need* the program running without an interruption. I don't want messy workarounds, like rebooting my application or using Mozilla component.

    Yours frustratedly

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: WebBrowser memory leak - is there any real solution?

    You don't need the web browser and you don't have to download the source.

    Like, 99% of the things the web browser component can do, you can do via HTTP WebRequests.

    And no, there's nothing you can do with the web browser component, in terms of memory.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    46

    Re: WebBrowser memory leak - is there any real solution?

    ...which in turn will force me to rewrite massive amount of code, not only I will have to handle postdata myself, I will also need to take care of the cookies. Not to mention the fact, that it will severely affect "quick peek into webbrowser" quasi-debugging I have to keep for users. And it seems that my another project that was based on saving actual rendering of the page is non-doable.

    Damn -__ Guess I will have to look into this.

    But since I got somebody's attention - is there any reason for WebBrowser to behave that way, or is it just a bug that magically got overseen for last 8 years of .NET development?
    Last edited by amras666; Mar 18th, 2011 at 08:07 PM.

  4. #4

    Re: WebBrowser memory leak - is there any real solution?

    Quote Originally Posted by weirddemon View Post
    You don't need the web browser and you don't have to download the source.

    Like, 99% of the things the web browser component can do, you can do via HTTP WebRequests.

    And no, there's nothing you can do with the web browser component, in terms of memory.
    OK I have had the same problem and i have tried to use httpweb reuest but can't seem to get it to work could you show me how i could do this with httpweb request: as you seem to understand it. many thanks

    My thread http://www.vbforums.com/showthread.php?t=644580
    Last edited by carspoo200; Mar 18th, 2011 at 08:13 PM.

  5. #5

    Re: WebBrowser memory leak - is there any real solution?

    Try upgrading your local install of Internet Explorer. I thought I heard one time that the WebBrowser was basically a managed copy of Internet Explorer. I could be mistaken though.

    EDIT: To the person above me, start a new topic for that if you would instead of hijacking someone else's thread.

  6. #6

    Re: WebBrowser memory leak - is there any real solution?

    Quote Originally Posted by amras666 View Post
    ...which in turn will force me to rewrite massive amount of code, not only I will have to handle postdata myself, I will also need to take care of the cookies. Not to mention the fact, that it will severely affect "quick peek into webbrowser" quasi-debugging I have to keep for users. And it seems that my another project that was based on saving actual rendering of the page is non-doable.

    Damn -__ Guess I will have to look into this.

    But since I got somebody's attention - is there any reason for WebBrowser to behave that way, or is it just a bug that magically got overseen for last 8 years of .NET development?
    put a Webbrowser1.Stop In the Webbrowser1_DownloadComplete event. seems to decrease the amount leaked.

    If your application starts not responding you can try the below to stop that:
    If you don't need to use your application while its navigating then....... put me.enabled = false at the start of the code and put this at the end me.enabled true
    Last edited by carspoo200; Mar 18th, 2011 at 09:59 PM.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    46

    Re: WebBrowser memory leak - is there any real solution?

    WebBrowser.Stop won't work,
    Upgrading to IE9 won't work.

    Seriously, Microsoft?

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: WebBrowser memory leak - is there any real solution?

    I think I know the cause of the apparent memory leak. It looks like the WebBrowser is not designed to deal with navigation without waiting for the document to complete. So Carspoo's method may work, and I found that this certainly did when placed in the Timer.Tick loop:

    Code:
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
    	WebBrowser1.Navigate(testUrl)
    End If
    Without that precaution, I was getting big leaks for sites with ads or other changing images such as microsoft.com and vbforums.com, but no significant leaks for sites with no images or only static images, such as google.com, en.wikipedia.org and about:blank. I think this implies that the WebBrowser is unable to clear its image cache until the document is ready, so that builds up.

    The WebBrowser control apparently wasn't designed to deal with repeated navigates to the same site without waiting for document completion. Maybe it would have been possible to do so, but I don't think you can call it a bug.

    BB

  9. #9
    New Member
    Join Date
    Apr 2011
    Posts
    1

    Re: WebBrowser memory leak - is there any real solution?

    Nice to find that i am not alone with the problem discussed here.
    I did a huge research but did not find any solution for it.

    But I have found that memory leak is caused by javascrpts that are on page you are loading. If you disable javascript execution for Webbrowser component there will be no memory leaks.

    In case if you use csEXWB webbrowser extended component you can esy disable scripts loading using wb.DownloadScripts=false.


    For me it is not solution as mu soft has to have javascripts executed on pages.

    We be glad if some one will find and post real solution here.

  10. #10
    New Member
    Join Date
    Jul 2011
    Posts
    2

    Re: WebBrowser memory leak - is there any real solution?

    I'm also having problems with this. I asked a similar question here, and they kinda blame the web site I'm dealing with. The problem is that even after I navigate to another site, the memory doesn't go down.

    I even tried erasing the previous TravelLog entries to no avail.

    I won't use HTTP request and that kind of stuff because in order to have access to AJAX content I would need to rewrite a whole web browser. Also, in order to have the aid of a visual tool, I would have to rewrite a whole web browser. Also, I would have to rewrite my whole application.

  11. #11
    New Member
    Join Date
    Jul 2011
    Posts
    2

    Re: WebBrowser memory leak - is there any real solution?

    I wonder if anyone was able to isolate the problem (write a small javascript that will cause the WebBrowser to leak).

  12. #12
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: WebBrowser memory leak - is there any real solution?

    I have an app that scrolls through web pages and gets this same issue. After testing out all the methods mentioned and a few more, there is no way to stop the leak. Even pages that complete fully, pages that are simple, all eventually run down the memory, it just takes longer. Perform a .Stop before changing url helps a little when sites fail to load.

    I have tried downloading all the content with a request and just setting the browser text instead, however this method does not work on my site that has ajax (which is a requirement for me!). The only answer I had was to reset my app after 24 hours. Another option is to run the browser under a seperate process and using remoting to get it to perform your actions, which could work for an automation project, and drop it after a few actions.

    For M$ its unlikely a user will sit on the same browser, on enough bad sites, for long enough for it to be an actual problem. Apps that process 100s of site changes however....

  13. #13

    Re: WebBrowser memory leak - is there any real solution?

    OK I found the solution to the problem!!!

    When I was searching for something else I came across a neat bit on c# code which i have converted to vb.net:

    Code:
        <DllImport("KERNEL32.DLL", EntryPoint:="SetProcessWorkingSetSize", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
        Friend Shared Function SetProcessWorkingSetSize(ByVal pProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Integer, ByVal dwMaximumWorkingSetSize As Integer) As Boolean
        End Function
        <DllImport("KERNEL32.DLL", EntryPoint:="GetCurrentProcess", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
        Friend Shared Function GetCurrentProcess() As IntPtr
        End Function
    Code to call when you want to reduce the memory:
    Code:
        Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            
            Dim pHandle As IntPtr = GetCurrentProcess()
            SetProcessWorkingSetSize(pHandle, -1, -1)
    
        End Sub
    Could put it in the webbrowser document complete event :P

    OR another solution is to just call:
    Code:
    GC.Collect()
    I just created a simple app with a web browser control to try and duplicate your results. What I found was that yes, every time you navigate to a page, the memory being used increases significantly. HOWEVER, this is NOT a memory leak, because if you keep navigating, you'll see that after a short while, the memory drops significantly, indicating that the garbage collector did it's thing. To prove it, I forced the Garbage Collector to collect after every time I called Navigate, and the overall memory used stayed put at almost the same amount after every navigate call.

    So while it DOES rack up memory every time you "Navigate" it's NOT a memory leak, and you the memory will be released. If it's raking up too quickly, just call GC.Collect()
    Last edited by carspoo200; Aug 29th, 2011 at 07:03 AM.

  14. #14
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: WebBrowser memory leak - is there any real solution?

    That does not solve the issue. The first API just tells the app to give back the memory that is not being used, and shove what is being used into VM instead of actual (working memory). It will not release anything that is being used, which in this case, is being used becuase of the memory leak! It is even documented by M$.

    The second gc.collect call just tells .Net to clear up memory, which makes no odds at all, as the component being referenced this case is not .Net anyway!

  15. #15

    Re: WebBrowser memory leak - is there any real solution?

    Quote Originally Posted by Grimfort View Post
    That does not solve the issue. The first API just tells the app to give back the memory that is not being used, and shove what is being used into VM instead of actual (working memory). It will not release anything that is being used, which in this case, is being used becuase of the memory leak! It is even documented by M$.

    The second gc.collect call just tells .Net to clear up memory, which makes no odds at all, as the component being referenced this case is not .Net anyway!
    Ok well I guess you know. But it seems to help me so far haven't come to any problems like before when the app memory could get to over 1Gb, then become extreemly slow.

  16. #16
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: WebBrowser memory leak - is there any real solution?

    I had already tried such things and I had got my app running for longer before running out of memory, but in the end, it still did it. My only course of action was to restart my app, which was possible as all my app does is scroll through web pages.

  17. #17
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: WebBrowser memory leak - is there any real solution?

    im having the same problem..... i guess still no solution

  18. #18
    Junior Member
    Join Date
    Jun 2012
    Posts
    20

    Re: WebBrowser memory leak - is there any real solution?

    Any one found the solution ?

    Memories leak like hell, it takes over 1GB of RAM to navigate just round 30 websites. Seriously what the hell is wrong with this Webbrowser control ? Why doesn't Mircosoft update or do any thing about this problem ?

  19. #19
    New Member
    Join Date
    Dec 2014
    Posts
    1

    Re: WebBrowser memory leak - is there any real solution?

    Quote Originally Posted by stuang View Post
    Any one found the solution ?

    Memories leak like hell, it takes over 1GB of RAM to navigate just round 30 websites. Seriously what the hell is wrong with this Webbrowser control ? Why doesn't Mircosoft update or do any thing about this problem ?
    I am trying below code in finally block and I hope it will works

    Dim loProcess As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess()
    Try


    loProcess.MaxWorkingSet = DirectCast(CInt(loProcess.MaxWorkingSet) - 1, IntPtr)
    loProcess.MinWorkingSet = DirectCast(CInt(loProcess.MinWorkingSet) - 1, IntPtr)
    Catch generatedExceptionName As System.Exception

    loProcess.MaxWorkingSet = DirectCast(CInt(1413120), IntPtr)
    loProcess.MinWorkingSet = DirectCast(CInt(204800), IntPtr)
    End Try

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width