我正在从一个流中读取数据并写入另一个流。我要复制的文件大小为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();
}
您可以尝试在写入的请求(webRequestWrite
)中将SendChunked
设置为true。否则,请求将在内部缓冲,直到请求流关闭,因此整个请求可以作为单个消息发送。基础资源需要支持此传输模式,并且可能需要您首先设置内容长度。这里的内部缓冲不同于AllowWriteStreamBuffering
,后者在重定向或身份验证请求的情况下缓冲用于重新发送的数据。
尝试用BufferedStream替换Stream。
http://msdn.microsoft.com/en-us/library/system.io.bufferedstream.read(v=vs.110).aspx
可能是重复的问题:
在C#中读取带有流的大型文本文件