2

What could be causing an out of memory exception in the code below? My program was running for a few hours and then died. The code only sends/receives a very small amount of data each time, so there are no huge files or strings going over the wire or coming back. The code sends and receives from the server every 3 seconds or so.

private void Read()
{
    string postData = "Name=John"
    using (HttpWebResponse response = SendRequest(new Uri(@"someWebSitehere"), postData))
    {
         Stream stream = response.GetResponseStream();
         StreamReader reader = new StreamReader(stream);
         responseFromServer = reader.ReadToEnd();  IT THROWS OUT OF MEMORY HERE
         stream.Close();
    }
}

private HttpWebResponse SendRequest(Uri uri, string postData)
{
    lock (SendRequestLock)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);

        req.Method = "POST";
        req.CookieContainer = new CookieContainer();
        req.Proxy = null;
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] byte1 = encoding.GetBytes(postData);

        // Set the content type of the data being posted.
        req.ContentType = "application/x-www-form-urlencoded";

        // Set the content length of the string being posted.
        req.ContentLength = byte1.Length;

        req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
        req.Method = "POST";
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        req.Headers.Add("Accept-Language: en-us,en;q=0.5");
        req.Headers.Add("Accept-Encoding: gzip,deflate");
        req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        req.KeepAlive = true;
        req.Headers.Add("Keep-Alive: 300");
        using (Stream stream = req.GetRequestStream())
        {
            stream.Write(byte1, 0, byte1.Length);
        }
        return (HttpWebResponse)req.GetResponse();
    }
}
1
  • What part triggers the OOM exception? Commented Oct 20, 2012 at 18:03

2 Answers 2

2

You'll want to dispose of the IDisposable classes Stream and StreamReader:

using (Stream stream = response.GetResponseStream())
{
    using (StreamReader reader = new StreamReader(stream)) 
    {
        responseFromServer = reader.ReadToEnd();  //IT THROWS OUT OF MEMORY HERE
    }
}

Classes that implement IDisposable generally have external resources that they will hang onto unless you call Dispose() (or, same thing, put it inside a using block). It's likely that those classes are leaking memory each time your block of code runs, hence the "out of memory" exception after some time.

It's worthwhile reading MSDN's notes on IDisposable.

Sign up to request clarification or add additional context in comments.

1 Comment

this ended up being the solution.
1

Have you checked Content-Length of the response. Maybe it is very huge. In this case you should read response stream part by part

5 Comments

I have not checked it exactly. What is considered a good size to start reading part by part?
It depends on what do you do with a server response.
I get text back and parse it.
In this case you can try to refactore you parser to take a Stream as input parameter. In some case this even better than parsing a string.
I will keep that in mind. The response is only 120 bytes usually so I probably won't have to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.