最佳缓冲区大小HttpWebResponse的响应流 [英] Optimal buffer size for response stream of HttpWebResponse

查看:1319
本文介绍了最佳缓冲区大小HttpWebResponse的响应流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是从一个流使用最佳缓冲区大小HttpWebResponse.GetResponseStream()?

What's the optimal buffer size to use with a stream from HttpWebResponse.GetResponseStream()?

在线实例不同的256B之多为5Kb。怎么办?我猜想缓冲区的大小可能情景。如果是的话有什么用缓冲区大小,什么类型的情况?

Online examples vary from 256b to as much as 5Kb. What gives? I guess buffer sizes might be situational. If so what are the situations to use what type of buffer size?

感谢。

推荐答案

真的,它不会是很大。

当然,如果你使用非常小的缓冲区,你可能通过层层做出一些额外的要求下去把字节(虽然流很可能这样做至少有一些缓冲 - 我不知道它的默认值是)。而且肯定的是,如果你使用真正的大缓冲区,会浪费一些内存,并介绍了一些碎片。既然你明明做的IO在这里,任何时候你通过调整缓冲区是要由IO时间为主收获。

Sure, if you use really small buffers, you may have to make a few extra calls down through the layers to get the bytes (though the stream is likely doing at least some buffering -- I don't know what it's defaults are). And sure, if you use really big buffers, you'll waste some memory and introduce some fragmentation. Since you're obviously doing IO here, any time you gain by tweaking the buffers is going to be dominated by the IO time.

一般情况下,我去了两者之间的2048(2k)和8192(8K)的功率。只要确保你知道自己在做什么,如果你去一个缓冲等于或大于85000个字节(它是那么的大对象,并受到不同程度的GC规则)。

As a general rule, I go with a power of two between 2048 (2k) and 8192 (8k). Just make sure you know what you're doing if you go with a buffer equal to or larger than 85,000 bytes (it's then a "large object" and subject to different GC rules).

其实,比缓冲区大小更重要的是你如何长时间拿着它。对于大对象堆之外的对象时,GC是非常善于处理非常短暂的对象(第0集的速度快),或很长时间存活的对象(第2代)。住足够长的时间去创1或2被释放之前,对象是相对更昂贵,而且通常更值得你花时间担心比有多大的缓冲区。

In fact, more important than the buffer size is how long you hold it. For objects outside of the large object heap, the GC is very good at dealing with very short-lived objects (Gen 0 collections are fast), or very long-lived objects (Gen 2). Objects that live long enough to get to Gen 1 or 2 before being freed are comparatively more costly, and usually much more worth your time worrying about than how big the buffer is.

最后要注意的:如果你认为你有因为缓冲区正在使用,测试它的大小的性能问题。这是不可能的,但谁知道,也许你的操作系统版本,网络硬件和驱动程序版本,一个奇怪的汇合,有一定的大小的缓冲区一些奇怪的问题。

One final note: if you think you have a performance issue because of the size of buffers you are using, test it. It's unlikely, but who knows, maybe you have an odd confluence of OS version, network hardware, and driver release that has some odd issue with certain-sized buffers.

这篇关于最佳缓冲区大小HttpWebResponse的响应流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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