Reputation: 6260
I saw a lot of examples of CopyStream implementation but I have question about buffer size when we copy streams.
Sample of one of CopyStreams implementation:
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
Questions are:
Related questions:
File IO with Streams - Best Memory Buffer Size - nice File IO answer. But what about in memory copying?
My case:
There is MemotyStream
which I create using ClosedXML workbook.SaveAs(memoryStream);
and it allocates huge amount of memory in managed heap. I've looked into sources and found that there is CopyStream method that uses 8 * 1024 buffer size. Could changing this size somehow decrease memory usage?
Note: Stream takes almost 1Gb of memory.
Upvotes: 2
Views: 5381
Reputation: 7961
If you are using .NET 4 you can do it simpler:
srcStream.CopyTo(dstStream);
But if you want/need to implement it yourself I would suggest smaller buffer (256B - 1KB) for memory streams and medium size buffer (10KB) for file streams. You can also make it dependent on size of the source stream for example 10% with some size limit of 1MB or so.
For files, the bigger the buffer the faster copy operation (to a degree) but less safe. For memory streams small buffer is pretty much just as effective as big one but easier on memory (if you copy a lot).
Upvotes: 6