Solved

Resolving memory leak in .NET WebBrowser control

Posted on 2008-10-29
12
3,404 Views
Last Modified: 2013-12-17
I have a C# desktop app (VS.NET 2005) which contains a form with a WebBrowser control on it.  Whenever I click on a link in the WebBrowser control, though, it uses memory.  Eventually, my app is consuming a large amount of memory that isn't released unless I close the program.

Any suggestions or alternatives to the WebBrowser control?  I have a deadline approaching, and need to resolve this.  Thank you all so much!  If you need further details, let me know, and I'll post them.
0
Comment
Question by:C_Sharp_Programmer
  • 7
  • 5
12 Comments
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22829599
Is the WebBrowser control viewing a page that you have created?

Do the pages being viewed contain JavaScript?
0
 
LVL 4

Author Comment

by:C_Sharp_Programmer
ID: 22833147
No, the page isn't one that I created; it doesn't matter what page I load in it...

Yes, I believe the page in question does have JavaScript on it... I've read that this can be an issue with the WebBrowser control; is there any way to deal with this, or is there an alternative to the WebBrowser control?
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22833335
Does the memory leak appear to occur if you point it at this website?

If it does then you could try to collect your garbage using a timer, say every minute.

public void timer1_Tick(object sender, EventArgs e)
{
   GC.Collect();
}

There isn't an alternative that I am aware of. I did a search for you and was hoping to find a FireFox one. I will keep an eye out for alternatives because this is something that I too would be interested in (but for different reasons).
0
 
LVL 4

Author Comment

by:C_Sharp_Programmer
ID: 22834312
The garbage collection doesn't seem to work...

I tried loading this site, and just navigating around it used up 5,000K memory...
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22834551
The WebBrowser control does need a lot of memory to run, but you should notice that it stops acquiring more memory at some stage. Do you think it would have exceeded 5,000K if you continue to look at more websites?

On my system the memory usage of a form with a WebBrowser control on it uses up to about 50,000K after clicking around a bit. But when it hits that amount it tends to drop again to 46,000K. The program started at about 23,000K and progressed onwards from there.

I then tried adding a timer with a 1 second interval, and called GC.Collect() to compare the results. This time the program started at about 24,000K and reached a maximum of 36,000K after the same number of menu items. Whenever it got to that amount it dropped down to around 26,000K.

What technique are you using to measure memory usage? I use "Process Explorer".
0
 
LVL 4

Author Comment

by:C_Sharp_Programmer
ID: 22834645
I've used the Windows Task Manager and Process Explorer...

For some reason my app doesn't release the memory.  I have a timer that refreshes the page / clicks a link ever so often.  When I first start the program, it's typically using 70,000K of memory... If I check it six hours later it's using 500,000K.  Even if I stop the timer and manually navigate to another website, the memory isn't released unless I exit the program completely.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 13

Expert Comment

by:numberkruncher
ID: 22834704
What version of IE are you using? Do you have a lot of plugins installed in IE?

I believe that the WebBrowser control is essentially an instance of IE.
0
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 22834816
After some more tries it turns out that the garbage collection is not having a bearing. However, each time I try it kind of selects a different maximum. Whenever it hits that maximum amount it always drops again. So on this computer it would appear to be working correctly. I am using VS2008 with .NET 3.5.

The only other web browser I have been able to find was a Mozilla ActiveX control  which looks as though it died in 2005 because it hasn't been touched since. A word of caution just in case you came across it; apparently it causes some serious security risks if you have got anything later than FireFox 1.0.

The only alternative I can think of is to create your own .NET wrapper around the Mozilla SDK. This is probably a mammoth task though!
0
 
LVL 4

Author Comment

by:C_Sharp_Programmer
ID: 22835091
I will look into the Mozilla ActiveX control... Basically, I just need something to display an HTML document, so I've been using the WebBrowser control.
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22835242
Have you tried your program on a different computer? If not, it might be worth a try.


I found another web browser ActiveX control; you have to purchase a license, but it looks to be inexpensive.

http://nbit.net.au/vpages.aspx?ID=HTML%20Viewer%20ActiveX
0
 
LVL 4

Author Comment

by:C_Sharp_Programmer
ID: 22872611
Thanks for the assistance.  I found some other alternatives as well.  The first is the Mozilla XULRunner, which is an embedded mozilla solution with the same basic API functionality as IE
https://developer.mozilla.org/en/XULRunner#Community

I also found GecoFX, which is a .NET wrapper for the firefox/gecko browser
http://code.google.com/p/geckofx/

In addition, it looks like WebKit is an alternative as well (the engine that Safari and Google Chrome are built on)
http://webkit.org/building/run.html
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22872936
Thanks for posting those links, they are quite interesting. The third link might help with a project that I am working on at the moment!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now