Solved

Resolving memory leak in .NET WebBrowser control

Posted on 2008-10-29
12
3,562 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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