This code causes some kind of memory leak. I assume it's caused by the new byte[]
.
But shouldn't the GC avoiding this? If the program runs long enough, the code will cause a OutOfMemoryException
using (var file = new FileStream(fileLoc, FileMode.Open))
{
int chunkSize = 1024 * 100;
while (file.Position < file.Length)
{
if (file.Length - file.Position < chunkSize)
{
chunkSize = (int)(file.Length - file.Position);
}
byte[] chunk = new byte[chunkSize];
file.Read(chunk, 0, chunkSize);
context.Response.BinaryWrite(chunk);
}
}