Thursday, June 03, 2004

Damn! Handle leaks on the CLR!

On the past few days, I'm dealing with some hard to find resource leaks. We seem to be pushing some areas CLR to their limit. You may say, "hey, how can you get resource leaks in a garbage collected environment"?.
Well, the simple code below will leak lots of handles.

private void MakeARequest()
{
HttpWebRequest wr
= (HttpWebRequest)WebRequest.Create("http://www.microsoft.com/");
wr.Proxy
= WebProxy.GetDefaultProxy();
wr.Proxy.Credentials
= CredentialCache.DefaultCredentials;
using (HttpWebResponse wrp = (HttpWebResponse)wr.GetResponse())
{
using (Stream s = wrp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
string st = sr.ReadToEnd();
sr.Close();
s.Close();
}
}
}
}



Did you see it? It isn't missing a single Close() or Dispose() call, and yet leaks lots of handles. The biggest the page, the more handles it leaks.
I've been using an evaluation version (10 days remaining...) of the great SciTech's .NET memory profiler and noticed that the CLR is forgetting to dispose some (a lot!) ManualResetEvent, Sockets, and some internal Streams and Readers.
After a while, I found that the culprit are the synchronous HttpWeb(Request, Response) methods, and, by calling the assynchronous versions, I was able to reduce it a lot. Even the returned stream, System.Net.ConnectStream have these problems.
The only problem is when the request throws an Exception, it still leaks lots of handles, and, by now, it seems that I can do nothing about it...

Comments:
I found the same problem calling a web service. It would leak 6 handles per call. I tried your example and it was leaking all 3 handles per request. (in the memory profiler, the undisposed instances are System.Net.ConnectStream and System.Threading.ManualResetEvent, one instance of each is not disposed per request) I am running under WinXP Professional with Service Pack 1. I tried the same code under Windows Server 2003 and the handle count goes up by about 70 handles once I start the requests, then stays level. (The program I use to test this makes 1000 requests in a loop) So, I was hoping you would be able to let me know what system your are running under so I can see if this is an option for me to try and reduce the leak.

Thanks,
Tim Carter
 
If you open the System.Net.ConnectStream and the HttpWebRequest code in Reflector and decompile it, you'll see that the synchronous versions of the methods are leaking the ManualResetEvent you're mentioning: change the code to call BeginGetResponse and EndGetResponse and, instead of using a StreamReader, read the ConnectStream with BeginRead and EndRead (the bug is on the Read method).
You'll notice that the Begin* method will return you an IAsyncResult. After the End* method you need to call IAsyncResult.WaitHandle.Close(), and the ManualResetEvent is gone. Each ConnectionStream.Read call will leak a ManualResetEvent, so, the bigger the page, the bigger the leak.
When some exception is thrown inside the code (e.g., an unavailable site), the leak is much worse, and it'll leak Sockets, Connections (IIRC) and other important objects.
I can't call MS and ask them for a patch, because some of my customers won't run a hotfix on their production environments, even if it's to stop a bug.
My solution, as you probably read on my blog, was to code a WinHTTP wrapper. It's the most beautiful thing in the world, but it works, it doesn't leak and it's supported on Win2k, WinXP and Win2k3, the environments my software supports.
In your case, the problem is worse: you're calling a Web Service from the generated VS.NET code, so you'll need to change it and instead of deriving it from : System.Web.Services.Protocols.SoapHttpClientProtocol, you'll need to derive from your own protocol class.
 
I have verified that this problem does not exist in VS2005 Beta 1. Has it been fixed in .Net 1.1 SP1?

Going async did not help much.

I do not want to interop to WinHttp.
 
It doesn't appear that the problem in fixed in Fx 1.1. SP1.

Did you find any sort of solution?
 
I found the same problem!!!
Is there soultion...
 
Try changing your framework settings to .NET 1.0 (Project Props - Build), try your app again and may some fun you have (given your app is compatible)...

-IZ
 
.

Free Anonymous Proxies

69.25.27.209:80
80.224.16.222:80
134.39.27.36:80
213.26.185.228:80
203.131.80.158:80
203.144.216.211
211.101.6.3:8080

More Free Proxies: Go to http://jeepee.biz/infopage, and get them at "Page1", "Page2" and "Page3"

Good luck!

jeepee.biz
 
I came across this problem and wasn't in a position to update my companies server to the newer framework. Also, I had at least 70 other websites on the server so wasn't able to gamble with memory issues!

With the help of the infastructure manager, we came across this link, hope it helps!

http://support.microsoft.com/?kbid=831138
 
Today is the gold für wow second day of 2009 ,world of warcraft gold it also a mesos special for me .cheap wow gold Because i have cheap maplestory mesos a chance to go to an english speech of LiYang and crazy to learn english follow him . He is a firendly,kindly person who make me feel good.wow gold kaufen What‘s more ,maple story mesos he very confidence .And he make me sure what he can do i also can do,as long as i make a determination and force myself to do it every second,Crazy just like him .wow geld I learn one setence from his book ever :maple story items If you want to succeed always force yourself to do more .I can't agree more with him .wow gold farmen To be honest ,when i was a littel girl i already fall in love with english.But what a pity i am poor in english ,Maple Story Account and it make me feel frustrating .So i want to give up many times ,but i can't as i still love it .So i tell mysefl :if you think you can you can wow leveling,and all your maple story power leveling hard work will pay off.wow power leveling Today i am very happy i can listien this wonderful speech of LiYang. I reap a great benifits from him .maple story money As he say :i am the best ,and every one can do it . world of warcraft power leveling Yes,i belive i can do it if i crazy as he
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?