What is the best memory buffer size to allocate to download a file from Internet?

后端 未结 5 1251
难免孤独
难免孤独 2021-01-01 23:50

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? A

5条回答
  •  粉色の甜心
    2021-01-02 00:15

    2k, 4k or 8k are good choices. It is not important how much is the page size, the change in speed would be really marginal and unpredictable.

    First of all, C# memory can be moved, C# uses a compacting generational garbage collector. There is not any kind of information on where data will be allocated.

    Second, arrays in C# can be formed by non-contiguous area of memory! Arrays are stored contiguously in virtual memory but contiguous virtual memory doesn't mean contiguous physical memory.

    Third, array data structure in C# occupies some bytes more than the content itself (it stores array size and other informations). If you allocate page size amount of bytes, using the array will switch page almost always!

    I would think that optimizing code using page size can be an non-optimization.

    Usually C# arrays performs very well but if you really need precise allocation of data you need to use pinned arrays or Marshal allocation, but this will slow down the garbage collector.

    Using marshal allocation and unsafe code can be a little faster but really it don't worth the effort.

    I would say it is better to just use your arrays without thinking too much about the page size. Use 2k, 4k or 8k buffers.

提交回复
热议问题