StringBuilder 增长超过 85k 并迁移到 LOH?

发布于 2024-12-07 17:37:59 字数 243 浏览 0 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-12-14 17:37:59

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 the ToString 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.

櫻之舞 2024-12-14 17:37:59

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文