Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8732905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:31:56+00:00 2026-06-13T09:31:56+00:00

What could be causing an out of memory exception in the code below? My

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:31:57+00:00Added an answer on June 13, 2026 at 9:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I took out all the things that I thought could be causing this white
I cannot figure out what could be causing this error. My Apache log is
I don't know what could be causing this issue, but for some reason, elements
I have searched the internet for an answer to what could be causing EXCEPTION_ACCESS_VIOLATION
Could someone please suggest why this is happening... I’ve got some code to pretty
Could someone help me to add A-atype B-btype C-ctype ONLY as a legend to
I'm writing a multithreaded program, which would crash when a particular exception was thrown.
I'm getting a memory access violation which i am not able to figure out.
I believe I have memory leak with some of my code that uses the
The following algorithm is run iteratively in my program. running it, without the two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.