title: WebRequest.GetRequestStream and LOH
body: <p>I am using code below to upload large file to server and noticed that copying FileStream to GetRequestStream the bytes array is created and hold in memory. This increase large object heap and I don't want it. Maybe someone know how to solve this?</p>
  
  <pre><code>Stream formData = new FileStream(.....)
  
      HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
      using (Stream requestStream = request.GetRequestStream())
      {             
       Helpers.CopyStream(formData, requestStream);
       requestStream.Close();
      }
  
       public static void CopyStream(Stream fromStream, Stream toStream)
              {
                  try
                  {
                      int bytesRead;
                      byte[] buffer = new byte[32768];
                      while (fromStream != null &amp;&amp; (bytesRead = fromStream.Read(buffer, 0, buffer.Length)) &gt; 0)
                      {
                          toStream.Write(buffer, 0, bytesRead);
                      }
                  }
                  catch (IOException)
                  {
                      //suppress empty stream response
                  }
              }
  </code></pre>
  
  <p>Memory profiler graph. bytes array create internally in GetRequestStream</p>
  
  <p><img src="https://i.stack.imgur.com/7Gp7R.png" alt="Memory profiler graph. bytes array create internally in GetRequestStream"></p>