Solved

Resolving memory leak in .NET WebBrowser control

Posted on 2008-10-29
12
3,594 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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