StringBuilder和内存问题

StringBuilder和内存问题

本文介绍了StringBuilder和内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个StringBuilder,其字符串包含12,000,000个字符。

当我执行ToString()时,我希望有大约25,000,000字节的

内存,然而,我最终得到了大约43,000,000字节。这几乎是双倍大小的b $ b。从ToString()返回的字符串实际上是大小

StringBuilder.Capacity,而不是StringBuilder.Length。它可能在StringBuilder.Length上有一个

字符串结尾字符,但它的实际内存

大小是StringBuilder.Capacity。


听起来不错吗?

I have a StringBuilder that has a string with 12,000,000 characters.
When I do a ToString(), I expect to have ~25,000,000 bytes worth of
memory, yet, I end up with ~43,000,000 bytes. That''s almost double
the size. The string returned from ToString() is actually of size
StringBuilder.Capacity, NOT StringBuilder.Length. It may have a
end-of-string character at StringBuilder.Length, but its actual memory
size is StringBuilder.Capacity.

Does this sound right?

推荐答案




容量StringBuilder是指在给定时间内可以在

StringBuilder中存储多少个字符。长度是StringBuilder在给定时间表示的字符串中表示的字符数。

长度和容量的最大值为2,147,483,647。


Unicode字符可以消耗2,3或4个字节。因此,根据

哪些字符存储在StringBuilder中,可以在StringBuilder中将12,000,000个字符转换为大约
$ b。

$ b 43,000,000字节。你将不得不迭代字符串中的字符

计算字节数,以查看字节数是否可以通过unicode字符的

存储来解释。


问候,


兰迪



Capacity of a StringBuilder is how many characters can be stored in the
StringBuilder at a given time. Length is how many characters are
represented in the string that the StringBuilder represents at a given time.
The maximum value of length and capacity is 2,147,483,647.

Unicode characters can consume 2, 3 or 4 bytes. Therefore, depending on
which characters are being stored in the StringBuilder, it would be possible
to have 12,000,000 characters in the StringBuilder that translate to about
43,000,000 bytes. You will have to iterate the characters in the string
counting the bytes to see if the number of bytes can be explained by the
storage of unicode characters.

Regards,

Randy




这篇关于StringBuilder和内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:37