I am reading data from one stream and writing to another. The file I want to copy is 1.4 GB of size. Why is the memory used by my program growing all the time while reading and writing. How can i prevent an out of memory exception?
//Write data from URL
HttpWebRequest webRequestWrite = (HttpWebRequest)WebRequest.Create(WriteUrl);
webRequestWrite.AllowReadStreamBuffering = false;
webRequestWrite.AllowWriteStreamBuffering = false;
webRequestWrite.Method = "PUT";
using (Stream responseStreamWrite = webRequest.GetRequestStream())
{
   int chunkSize = 4194304;
   byte[] buffer = new byte[chunkSize];
   int bytesRead = 0;
   int totalBytes = 0;
   //Read data from URL
   HttpWebRequest webRead = (HttpWebRequest)WebRequest.Create(ReadUrl);
   webRead.AllowReadStreamBuffering = false;
   webRead.AllowWriteStreamBuffering = false;
   using (WebResponse webResponseread = webRead.GetResponse())
   using (Stream responseStreamRead = webResponseread.GetResponseStream())
   {
      while ((bytesRead = responseStreamRead.Read(buffer, 0, chunkSize)) > 0)
      {
        totalBytes += bytesRead;
        responseStreamWrite.Write(buffer.Take(bytesRead).ToArray(), 0, bytesRead);
        responseStreamWrite.Flush();
        responseStreamRead.Flush();
      }
   }
   responseStreamWrite.Flush();
}
 
     
     
    