StringBuilder 增长超过 85k 并迁移到 LOH?
可能的重复:
StringBuilder的容量如何变化?
假设一个 StringBuilder 被分配,然后它增长到超过85k,它会被移到大对象堆吗?
Possible Duplicate:
How does StringBuilder's capacity change?
Let's say a StringBuilder is allocated and then it grows to over 85k, will it get moved over to the Large Object Heap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
StringBuilder
不会“增长”。在 4.0 之前的 SB 中,它只是分配一个新的更大的缓冲区并将内容从旧的复制到新的。所以最后是的,内部缓冲区被移至 LOH。 SB 对象不是,因为它非常小(为了简单起见,它可能只是对缓冲区和缓冲区中字符串长度的引用。它有点复杂,因为它在之后实现了写时复制使用
ToString
方法,因此ToString
方法使缓冲区只读并将其作为字符串返回,并且对 SB 的任何其他写入都会复制该缓冲区。在 4.0 中,SB 使用类似于缓冲区链接列表(称为“绳索”)的东西,并且它们总是足够小,不会进入 LOH。
StringBuilder
doesn't "grow".In pre-4.0 SB, it simply allocated a new bigger buffer and copied the content from the old to the new. So in the end yes, the internal buffer was moved to LOH. The SB object not, because it's very small (to make it simple, it could have been simply a reference to the buffer and the length of the string in the buffer. It was a little more complex because it implemented copy-on-write after using the
ToString
method. So theToString
method made the buffer read-only and returned it as a string, and any other write to the SB duplicated the buffer).In 4.0 SB uses something like a linked list of buffers (it's called a "rope"), and they are always small enough not to go to LOH.
StringBuilder
本身不会在 LOH 上分配,但其内部字符串缓冲区会分配。分配器不知道哪些对象正在分配其数据。它只知道大于某个最大大小的分配将进入 LOH。The
StringBuilder
itself won't be allocated on the LOH, but its internal string buffer will be. The allocator doesn't know anything about what objects are allocating its data. It just knows that an allocation larger than some maximum size will go to the LOH.