title: Must I explicitly close the ResponseStream of an async http request?
body: <p>I have an Azure worker role running that, among other functions, makes some HTTP requests once every 80 seconds or so. This happens continuously. As we scale up, it might be making a lot more HTTP requests, so I wrote the code to use BeginGetResponse, EndGetResponse and a callback. The problem is... we have a memory leak somewhere. As this process runs, it slowly but surely loses memory until it runs out completely. Sometimes the GC will kick in and free up some unused objects, but it continues its slow downward trend..</p>
  
  <p>When our callback executes and we finish the request with EndGetResponse(), <em>we don't touch the response stream.</em> All we need to know is the HTTP status code, which we save for our own records. We never call GetResponseStream() and subsequently Close() it. We <em>do</em> Close() the HttpWebResponse.</p>
  
  <p>My question is: do we need to do something with the Response Stream and then Close() it? Would not doing so cause a memory leak? All of the MS examples/other SO discussions that I've seen do something with the stream. I'm wondering if we should add GetResponseStream().Close()...</p>
  
  <p>Here's the code:</p>
  
  <pre><code>// the request state class, passed along with async request
  public class RequestState
  {             
      public HttpWebRequest Request { get; set; }
      public HttpWebResponse Response { get; set; }
  
      // some other properties to track which request this is..
  }
  
  ...... in some other class .....
  
  // code to perform the request
  public void DoHttpRequest() 
  {
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://myurl.....");
      RequestState state = new RequestState(req); // this just sets the Request property on RequestState
      req.BeginGetResponse(OnRequestComplete, state);
  }
  
  // the callback, request has finished
  public void OnRequestComplete(IAsyncResult result)
  {
      RequestState state = (RequestState)result.AsyncState;
      HttpWebRequest req = state.Request;
      state.Response = (HttpWebResponse)req.EndGetResponse(result);
  
      // we do not care about the body of the response
      // all we want is the status code, which we store somewhere else..
  
      if (state.Response.StatusCode == HttpStatusCode.OK || state.Response.StatusCode == HttpStatusCode.Created)
      {
           // comm was successful
           // save this result code somewhere...
      }
      else if (state.Response.StatusCode == HttpStatusCode.RequestTimeout || state.Response.StatusCode == HttpStatusCode.GatewayTimeout)
      {
            // comm timed out
            // save this result code somewhere..
      }
      else
      {
            // something else, comm failed
            // save this result code somewhere..
      }
  
      // we've got the relevant data from the HttpWebResponse object, dispose of it
      state.Response.Close();
  }
  </code></pre>
  
  <p>Thank you!</p>