AJAX .NET CollapsiblePanel inside a gridview is causing memory leaks? - c#

One of our ASP.NET pages has a significant memory leak. After a process of manual enable/disabling, I narrowed down the issue to a set of collapsible panels which are dynamically built on the code page and placed into an UpdatePanel which is already on the page.
Using sIEve, I was able to see that this control apparently is creating a large amount of script tags, each having the following form (ignore the sIEve hook attribute):
<SCRIPT type=text/javascript __sIEve_hookedNode="true">Sys.Application.add_init(function() {
$create(AjaxControlToolkit.CollapsiblePanelBehavior, {"ClientStateFieldID":"ctl00_Main_branchPanelExtender_ClientState","CollapseControlID":"ctl00_Main_btnBranchExpander","Collapsed":true,"CollapsedSize":0,"CollapsedText":"* ; ","ExpandControlID":"ctl00_Main_btnBranchExpander","ExpandedSize":100,"ExpandedText":"Collapse","ScrollContents":true,"TextLabelID":"ctl00_Main_btnBranchExpander","id":"ctl00_Main_branchPanelExtender"}, null, null, $get("ctl00_Main_BranchPanel"));});</SCRIPT>
I think this is the problem, but I'm not completely sure. sIEve isn't reporting it as a memory leak, but the DOM nodes are definitely increasing and the page size grows out of control. I'm also not sure what to do about it. My best guess is that the corresponding remove_init isn't being called (or doesn't exist) but I can't find either function call in the AjaxControlToolkit library project.
Has anyone else encountered this problem or have any ideas on how to proceed with fixing it?
Edit: Browser Info
This is mainly IE 6, of course, but I also note the issue with FireFox 3. Opera and Chrome don't seem to have the memory leak, but Chrome doesn't render the controls right either.
Further Info:
I've noticed a similar problem on another page using HoverMenuExtender. It seems like the DOM just grows each time the UpdatePanel is posted and IE never frees the memory. Is there maybe a way to catch which items are holding over and manually remove them, or am I being overly cautious and IE will eventually free the memory up if needed?

Well, no answers on this one and I doubt one is coming. The problem can be answered very briefly though as "It's IE6." Sometimes certain DOM elements (the selection elements in an Option tag seem particularly prone) just don't clean up, so they fill the DOM up over time. Nasty, but just another thing to manage in IE6.

Related

How to run WebBrowser in another process, but in the original application

My application is something like Chrome or Firefox and allow to create and destroy tabs with WebBrowser object. It is very rough description, but close to understand what app is doing.
The problem is that the WebBrowser object is based on Internet Explorer (IE) and IE has a memory leak in deep of .Net unmanaged memory if closed site have some JS code or images (which is 99.99% of sites).
I tried every solution from web and finally I have got a confirmation from MS that this issue based on IE architecture and it can't be fixed by any trick from the code perspective (object cleaning or calling GC).
So, I started thinking in different way to solve the problem.
The problem is that the leak happened in unmanaged memory of .Net WebBrowser object and .Net does not clear it properly when WebBrowser object destroyed. But it happens in my application process ID.
So, I am looking for some option to separate memory for my application and for WebBrowser object (create it in a separate process). I know that Chrome is working in similar way (one process per one tab) which makes this idea realistic at least. But I can't find any info how can I do it to display WebBrowser in my app as it was before.
Thanks in advance for help!

Too many WebBrowsers causing crash in mshtml.dll

The program I am writing takes a very large amount of html text, splits it up, and displays it in multiple WebBrowser controls. It is split up because the amount of text is so large that it cannot be placed in a single string variable, let alone a single WebBrowser control. This all seems to work well, up to a certain point. The point where it breaks seems to be after creating about 72 WebBrowser controls, with about 2000 lines of html in each one. Each browser is instantiated like so:
var browser = new WebBrowser();
browser.Navigate("about:blank");
browser.Document.OpenNew(false);
browser.Document.Write(node.HTML);
After instantiating all of the webbrowsers, the form will load and show itself. Note that only one WebBrowser is visible at a time, the rest are tabbed. After about 5 seconds (while the winforms event loop is basically idling) the program will crash due to an exception in mshtml.dll. Below is the call stack that i pulled:
and the disassembler:
The mouse cursor is where the program breaks. I've doubled checked all of the html and it is all valid. I can load up the first half and the second half of the html separately without crashing. Only when all is loaded it will fail. I've also tried cutting down the number of browsers in half, and doubling the amount of html they individually show, which ended up with the same result. Also tried dynamically loading the html in each web browser when they are first opened.
It seems to me there is some kind of shared memory between all web browser instances, and it eventually just gets corrupted or something.
I'm pretty much at a loss here. I was thinking of possibly replacing the WebBrowser with CefSharp browser, but I don't want to go that route just yet if its not necessary.

.Net WebBrowser Control memory leak

My c# application uses .net WebBrowser. I need to close child control everytime and I have noticed WebBrowser is not getting disposed and RAM consumption is increasing heavily with each call (around 10 mb on each new control declaration) and application crashes in sometime with OutOfMemoryException. Searched over forums but couldn't find a clean solution.
Tried SetProcessWorkingSetSize(pHandle, -1, -1); but it doesn't reduce virtual memory though RAM uses will be reduced and its not a clean way of overcoming the issue.
Seems this issues exists since years, for more details look at this thread How to get around the memory leak in the .NET Webbrowser control?
Any suggestions ? Tried almost everything but no success yet.
Thanks,
Abhinav
I don't know whether your situation is similiar to mine but I have wasted three days for that weird problem.
My application was performing a search on a web page and my code was like that;
1.Open web page
for(1000 times)
{
2.Write input and click search button.
3.Check the result.
}
As you see my program opens the web page and makes repeated searches.Here opening the page (navigaiton) occurs only one time. My program's memory consumption was continuously increasing even above 1 GB! Then I tried putting the navigation inside the loop it worked.
for(1000 times)
{
1.Open web page
2.Write input and click seach button.
3.Check the result.
}
I exactly don't know the reason but reusing the same page for a long time was the cause of my problem. I hope it helps.

Why is WebBrowser component eating so much ram and how to flush WebBrowser cache

I am using a custom text box (read only) that consist of 2 WebBrowser components. It's displaying text with complicated layout (Generating a html code was a simplest option to make such a text viewer).
I am also binding the mousedown event on all components:
foreach (HtmlElement html in webBrowser1.Document.All)
{
html.MouseDown += new HtmlElementEventHandler(Scrollback_Clicked);
}
this happens after each time where I finish loading of html source. (this is irrelevant but it's the only customization of WebBrowser control I made, just in case it was the reason why it doesn't work)
However, after several hundreds of reloads the text box eats about 2GB of ram, I suspect it's a cache the browsercontrol has implemented, which stores all HtmlDocuments that were generated so far.
Is there a way to disable or flush the cache of WebBrowser control?
First off, you're probably leaking event handlers, considering the way you're registering them and then failing to dispose them. That might cause a slight performance problem, but it obviously doesn't explain the consumption of an excessive 2 GB of RAM.
Anyway, the WebBrowser control is based on Internet Explorer, so it's possible it inherits a memory leak from there. You may also be onto something with the caching theory. There is a knowledge base article that discusses precisely this problem and how to solve it: How to clear the cache when your application hosts a WebBrowser control in Visual C# .NET.
Basically, it suggests that you manually flush the browser cache using the WinInet APIs. But that seems like a bit of a sledgehammer solution to me. It's solving a local problem using a global solution, which should raise a few red flags.
If I were you, I'd be rethinking my application's design so as to avoid this problem altogether. RichTextBox controls don't have any show-stopper memory leaks. :-)

ASP.NET pages never stop loading

We have a problem with many of our website written C# ASP.NET.
The problem is that often a C# page will never fully load from the server.
The page content itself load fine but Images but more annoying Scripts (Javascript) seam to hang and never come down and depending on the content it might lock up the page on Ajax Postback.
This problem is not limited to a single server as it happens on development machines as well as pre production and production servers.
The development machine are just using the inbuilt VS IIS Instance.
All pages that have this problem use ASP.NET Update Panels with varying versions of AJAX Toolkit.
Thanks
One of the possible reasons is that your postbacks are being triggered on some events which are either being fired continuously. Another possible cause is that the page life cycle is not getting completed (e.g. infinite loop). If you can post some code, you would get precise answers.
Thanks,
Vamyip
Try using asp.net trace to see which page event is taking longer. Also try using fiddler from client side to see the real traffic and error codes. If you share the fiddler logs I may be able to tell more about what's happening.
You can check the time used by all the resourced that are loaded on the page using Firefox and firebug add-on
Anyway the updatepanel doesn't work very well with heavy pages : it posts the whole page to the server and get the whole rendered HTML even if you need refresh a small portion of the page.
if you can you should replace the updatepanel with async Jquery call to the get the the response as JSON and then populate the page as you wish or try to use anothe approach similar to the update panel.
have a look at:
http://www.codeproject.com/KB/aspnet/partialRendering.aspx

Categories