从Internet下载文件分配的最佳内存缓冲区大小是多少? [英] What is the best memory buffer size to allocate to download a file from Internet?

查看:208
本文介绍了从Internet下载文件分配的最佳内存缓冲区大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为从Internet下载文件分配的最佳内存缓冲区大小是多少?一些样本表示应该为1K。好吧,我通常需要知道为什么会这样吗?如果我们下载小的 .PNG 或大的 .AVI ,又有什么区别?

What is the best memory buffer size to allocate to download a file from Internet? Some of the samples said that it should be 1K. Well, I need to know in general why is it? And also what's the difference if we download a small .PNG or a large .AVI?

Stream remoteStream;
Stream localStream;
WebResponse response;

try
{
    response = request.EndGetResponse(result);

    if (response == null)
        return;

    remoteStream = response.GetResponseStream();

    var localFile = Path.Combine(FileManager.GetFolderContent(), TaskResult.ContentItem.FileName);
    localStream = File.Create(localFile);

    var buffer = new byte[1024];
    int bytesRead;

    do
    {
        bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
        localStream.Write(buffer, 0, bytesRead);
        BytesProcessed += bytesRead;
    } while (bytesRead > 0);
}


推荐答案

至少使用4KB。这是Windows的正常页面大小(即Windows本身管理内存的粒度),这意味着.Net内存分配器不需要将4KB页面分解为1KB分配。

Use at least 4KB. It's the normal page size for Windows (i.e. the granularity at which Windows itself manages memory), which means that the .Net memory allocator doesn't need to break down a 4KB page into 1KB allocations.

当然,使用64KB的块会更快,但只有一点点。

Of course, using a 64KB block will be faster, but only marginally so.

这篇关于从Internet下载文件分配的最佳内存缓冲区大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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