Re: WebResponse.Close() not disposing unmanaged resource?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




Hi Chris,

don't know if makes any change, but the WebResponse class implements the
IDisposable interface, so you could try a using statement:

using (System.Net.WebResponse resp=req.GetResponse()) {
// do some stuff with resp if you want.
}

If there's any cleanup code in the WebRespone.Dispose implementation, it
will be executed as soon as the using block is left and it will be executed
even if an exception is thrown.

Michael



<cmbardon@xxxxxxxxxxxxxxxxxxxx> schrieb im Newsbeitrag
news:1131564602.694642.104070@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>I have a .net app that uses a lot of calls to System.Net.WebRequest,
> and it appears to be leaking memory. I've spent a couple of days
> working with the excellent .net memory profiler from Scitech
> (http://www.scitech.se/) trying to find the leak, and all I've managed
> to find is that the win32 (unmanaged) heap seems to keep growing. I
> wrote this (pretty non-realistic) code in a test app to try duplicating
> the problem:
>
> while(true)
> {
> try
> {
> System.Net.WebRequest
> req=System.Net.WebRequest.Create(@"http://chris/TestWebService/TestService.asmx?WSDL";);
> System.Net.WebResponse resp=req.GetResponse();
> resp.Close();
> }
> catch(Exception ex)
> {
> MessageBox.Show(ex.ToString());
> }
> }
>
>
> Note that this was just inside a button click event of a winform
> application. The exception condition never got hit, and the app just
> kept hammering away at my local web server. Checking the profiler, I
> saw the number of undisposed instances of System.Net.HttpWebResponse,
> System.Net.ConnectStream, and System.Threading.ManualResetEvent
> increasing with each iteration. Shouldn't the resp.Close() statement
> release the resources? There's nothing in the documentation about
> doing any sort of disposal on the request object, so it appears that
> only the response needs to be disposed, and so far, that doesn't seem
> to be happening. I've also tried the above code casting the response to
> an HTTPWebResponse, but that has no effect. I also tried removing the
> resp.Close() statement, which had no effect (the undisposed objects
> still showed up in the profiler). Is there something else I need to do
> to fix this memory leak?
>
> Thanks for your help,
>
> Chris
>


.



Relevant Pages

  • Re: performance in a citrix environment
    ... The .Net memory profiler is a very useful tool for working out if you have ... Open a form in the app and do some work, ... I plan to use the .NET Memory Profiler ...
    (microsoft.public.dotnet.framework.performance)
  • WebResponse.Close() not disposing unmanaged resource?
    ... I have a .net app that uses a lot of calls to System.Net.WebRequest, ... and it appears to be leaking memory. ... Checking the profiler, I ...
    (microsoft.public.dotnet.framework)
  • Re: CLR Profiler Question
    ... applications; however, I am puzzled as to the memory allocation size that the ... This is what the CLR Profiler says is the memory utilization for the two ... Hello World app: 13 MB ...
    (microsoft.public.dotnet.general)
  • Re: D7 Ent Error: Unable to write to
    ... you have a memory leak,un-initiated variable or using an object that has ... I also had an app for the longest time would do some strange things ... Chris L. wrote: ... The .exe also executed without error on the WinXP ...
    (comp.lang.pascal.delphi.misc)
  • Re: Linux 2.6.9-ac16
    ... On Fri, 2004-12-17 at 16:45 +0100, Chris Ross wrote: ... > deciding which task to kill (like me he got fed up with it picking the ... wait a bit for the memory to be freed and become available to the ... Andrea's fix and the selection changes should go into 2.6.10, ...
    (Linux-Kernel)