在下载过程中ASP.net内存使用情况 [英] ASP.net memory usage during download

查看:148
本文介绍了在下载过程中ASP.net内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作地点的ASP.net网站,code以下块负责处理文件下载(注:Response.TransmitFile这里不使用,因为下载的内容正在从一个流zip文件):

On an ASP.net site at my place of work, the following chunk of code is responsible for handling file downloads (NOTE: Response.TransmitFile is not used here because the contents of the download are being streamed from a zip file):

private void DownloadFile( Stream stream)
{
        int bytesRead;
        int chunkSize = 1048576; //1MB

        byte[] readBuffer = new byte[chunkSize];
        while ((bytesRead = stream.Read(readBuffer, 0, readBuffer.Length)) != 0)
            {
                if(!Response.IsClientConnected)
                    break;
                byte[] chunk = new byte[bytesRead];
                Array.Copy(readBuffer,0,chunk,0,bytesRead);
                Response.BinaryWrite(chunk);
                Response.Flush();
        }
        stream.Close();
}

我们的用户经常下载多几百MB的文件,它可以快速咀嚼服务器内存pretty。我的假设是,这是由于响应缓冲。这是否有意义?

Our users frequently download multi-hundred MB files, which can chew up server memory pretty fast. My assumption is that this is due to response buffering. Does that make sense?

我刚刚读到有关Response对象的缓冲属性。如果我设置为false,将那prevent的Response.BinaryWrite()在内存中缓冲数据调用?一般情况下,什么是限制在这种情况下内存使用情况的好办法?也许我应该从zip传输到一个临时文件,然后调用Response.TransmitFile()?

I've just read about the 'buffer' property of the Response object. If I set that to false, will that prevent the Response.BinaryWrite() calls from buffering the data in memory? In general, what is a good way to limit memory usage in this situation? Perhaps I should stream from the zip to a temporary file, then call Response.TransmitFile()?

编辑:除了可能的解决方案,我在code以上的内存使用量的问题present的解释很感兴趣。为什么会这样消耗远远超过1MB,即使Response.Flush被称为在每次循环迭代?难道只是在每次循环迭代时(而没有得到GC'd马上),还是有别的东西在工作中不必要的堆分配?

In addition to possible solutions, I'm very interested in explanations of the memory usage issue present in the code above. Why would this consume far more than 1MB, even though Response.Flush is called on every loop iteration? Is it just the unnecessary heap allocation that occurs on every loop iteration (and doesn't get GC'd right away), or is there something else at work?

推荐答案

下面是一些code,我的工作在这一点。它使用8000字节的缓冲区发送文件块中。在一个大文件的一些非正式的测试表明在分配内存显著下降。

Here is some code that I am working on for this. It uses an 8000 byte buffer to send the file in chunks. Some informal testing on a large file showed a significant decrease in memory allocated.

int BufferSize = 8000;
FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
try {
  long fileSize = stream.Length;

  long dataLeftToRead = fileSize;
  int chunkLength;
  buffer = new Byte[BufferSize];

  while (dataLeftToRead > 0) {
    if (!Response.IsClientConnected) {
      break;
    }
    chunkLength = stream.Read(buffer, 0, BufferSize);

    Response.OutputStream.Write(buffer, 0, chunkLength);
    Response.Flush();

    dataLeftToRead -= chunkLength;
  }
}
finally {
  if (stream != null) {
    stream.Close();
}

编辑解决语法错误和遗漏值

edited to fix a syntax error and a missing value

这篇关于在下载过程中ASP.net内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆