从流写入到另一个读取时内存超过

时间:2014-09-28 00:24:04

标签: c# memory stream out-of-memory buffer

我正在从一个流中读取数据并写入另一个流。 我要复制的文件大小为1.4 GB。 为什么我的程序使用的内存在读写时总是在增长。 如何防止内存不足异常?

//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();
}

2 个答案:

答案 0 :(得分:1)

您可以尝试在写入的请求(SendChunked)上将webRequestWrite设置为true。否则,请求将在内部缓冲,直到请求流关闭,因此整个请求可以作为单个消息发送。底层资源需要支持此传输模式,可能需要先设置内容长度。此处的内部缓冲与AllowWriteStreamBuffering不同,{{1}}在重定向或身份验证请求的情况下缓冲数据以进行重新发送。

答案 1 :(得分:0)

尝试使用BufferedStream替换Stream。

http://msdn.microsoft.com/en-us/library/system.io.bufferedstream.read(v=vs.110).aspx

可能是重复的问题:

Reading large text files with streams in C#