Solved

Resolving memory leak in .NET WebBrowser control

Posted on 2008-10-29
12
3,382 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

10 Experts available now in Live!

Get 1:1 Help Now