Search code examples
c#.netwebrequest

WebRequest.GetRequestStream and LOH


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?

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 && (bytesRead = fromStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        toStream.Write(buffer, 0, bytesRead);
                    }
                }
                catch (IOException)
                {
                    //suppress empty stream response
                }
            }

Memory profiler graph. bytes array create internally in GetRequestStream

Memory profiler graph. bytes array create internally in GetRequestStream


Solution

  • You can use the HttpWebRequest.AllowWriteStreamBuffering to disable internal buffering:

        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    
        request.AllowWriteStreamBuffering = false;
    
        using (Stream formData = File.Open(fileName, FileMode.Open))
        using (Stream requestStream = request.GetRequestStream())
        {
            formData.CopyTo(requestStream, 32768);
        }