473,765 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StringBuilder and internal memory question

DV
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.C apacity, NOT StringBuilder.L ength. It may have a
end-of-string character at StringBuilder.L ength, but its actual memory
size is StringBuilder.C apacity.

Does this sound right?

Mar 1 '06 #1
15 2813
DV,
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.C apacity, NOT StringBuilder.L ength. It may have a
end-of-string character at StringBuilder.L ength, but its actual memory
size is StringBuilder.C apacity.


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
Mar 1 '06 #2

"DV" <da*****@gmail. com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.com.. .
|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.C apacity, NOT StringBuilder.L ength. It may have a
| end-of-string character at StringBuilder.L ength, but its actual memory
| size is StringBuilder.C apacity.
|
| Does this sound right?
|

No it doesn't, I really don't know where you got this value from but it's
not the size of the SB. The Capacity and Length property values are the
number of characters the SB can hold and the actual number it holds. Note
the "number of characters", not the number of bytes.
If you create a stringbuilder like this:

string s = new String('\u0306' , 12000000);
StringBuilder sb = new StringBuilder(s );

you will end with a SB buffer (a String) with a Capacity of 16777216 and a
Length of 12000000.
The length of the char[] (the string backing store) in bytes is 33554432.
Note that the string backing store is a char[], so the buffer size does not
depend encoding used as the other replier suggests, a char is fixed 2 bytes
in .NET.

Willy.



Mar 1 '06 #3

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uB******** ******@tk2msftn gp13.phx.gbl...
|
| "DV" <da*****@gmail. com> wrote in message
| news:11******** **************@ j33g2000cwa.goo glegroups.com.. .
||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.C apacity, NOT StringBuilder.L ength. It may have a
|| end-of-string character at StringBuilder.L ength, but its actual memory
|| size is StringBuilder.C apacity.
||
|| Does this sound right?
||
|
| No it doesn't, I really don't know where you got this value from but it's
| not the size of the SB. The Capacity and Length property values are the
| number of characters the SB can hold and the actual number it holds. Note
| the "number of characters", not the number of bytes.
| If you create a stringbuilder like this:
|
| string s = new String('\u0306' , 12000000);
| StringBuilder sb = new StringBuilder(s );
|
| you will end with a SB buffer (a String) with a Capacity of 16777216 and a
| Length of 12000000.
| The length of the char[] (the string backing store) in bytes is 33554432.
| Note that the string backing store is a char[], so the buffer size does
not
| depend encoding used as the other replier suggests, a char is fixed 2
bytes
| in .NET.
|
| Willy.

To add to my previous reply, if you create a SB like this:

StringBuilder sb = new StringBuilder(s , 12000000);

Your buffer will be 24000000 bytes (SB Capactity=Lengt h=12000000). The
reason for this is that now you create a SB with a predefined length, while
in the previous sample, the SB starts with a Capactity of 16 and expands by
doubling it's capacity each time it gets filled completely.
Willy.

Mar 1 '06 #4
DV <da*****@gmail. com> wrote:
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.C apacity, NOT StringBuilder.L ength. It may have a
end-of-string character at StringBuilder.L ength, but its actual memory
size is StringBuilder.C apacity.

Does this sound right?


Its size in terms of memory consumption is indeed represented by the
capacity.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 1 '06 #5
Randy A. Ynchausti <ra************ *@msn.com> wrote:

<snip>
Unicode characters can consume 2, 3 or 4 bytes.


That's true in some encodings, but .NET internally uses UTF-16, which
is exactly 2 bytes per character. Surrogate pairs are treated as two
characters when it comes to things like String.Length.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 1 '06 #6
Willy Denoyette [MVP] <wi************ *@telenet.be> wrote:
No it doesn't, I really don't know where you got this value from but it's
not the size of the SB. The Capacity and Length property values are the
number of characters the SB can hold and the actual number it holds. Note
the "number of characters", not the number of bytes.
If you create a stringbuilder like this:

string s = new String('\u0306' , 12000000);
StringBuilder sb = new StringBuilder(s );

you will end with a SB buffer (a String) with a Capacity of 16777216 and a
Length of 12000000.


Yes - but if you then call ToString() on that StringBuilder, you'll
find that the string only has a Length of 12000000 but takes up
2*16777216+over head bytes. In other words, StringBuilder doesn't trim
the string to its length before returning it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 1 '06 #7
That's why I said....
The length of the char[] (the string backing store) in bytes is 33554432.
Willy.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
| Willy Denoyette [MVP] <wi************ *@telenet.be> wrote:
| > No it doesn't, I really don't know where you got this value from but
it's
| > not the size of the SB. The Capacity and Length property values are the
| > number of characters the SB can hold and the actual number it holds.
Note
| > the "number of characters", not the number of bytes.
| > If you create a stringbuilder like this:
| >
| > string s = new String('\u0306' , 12000000);
| > StringBuilder sb = new StringBuilder(s );
| >
| > you will end with a SB buffer (a String) with a Capacity of 16777216 and
a
| > Length of 12000000.
|
| Yes - but if you then call ToString() on that StringBuilder, you'll
| find that the string only has a Length of 12000000 but takes up
| 2*16777216+over head bytes. In other words, StringBuilder doesn't trim
| the string to its length before returning it.
|
| --
| Jon Skeet - <sk***@pobox.co m>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too
Mar 1 '06 #8
Hit send too fast.
The String reference returned from SB.ToString() is just the same reference
to the existing String object, trimming would involve the creation of a new
string, this would increase the memory footprint and would take a
performance hit.

Willy.

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:OP******** ******@TK2MSFTN GP14.phx.gbl...
| That's why I said....
| The length of the char[] (the string backing store) in bytes is 33554432.
|
|
| Willy.
|
|
|
| "Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
| news:MP******** *************** *@msnews.micros oft.com...
|| Willy Denoyette [MVP] <wi************ *@telenet.be> wrote:
|| > No it doesn't, I really don't know where you got this value from but
| it's
|| > not the size of the SB. The Capacity and Length property values are the
|| > number of characters the SB can hold and the actual number it holds.
| Note
|| > the "number of characters", not the number of bytes.
|| > If you create a stringbuilder like this:
|| >
|| > string s = new String('\u0306' , 12000000);
|| > StringBuilder sb = new StringBuilder(s );
|| >
|| > you will end with a SB buffer (a String) with a Capacity of 16777216
and
| a
|| > Length of 12000000.
||
|| Yes - but if you then call ToString() on that StringBuilder, you'll
|| find that the string only has a Length of 12000000 but takes up
|| 2*16777216+over head bytes. In other words, StringBuilder doesn't trim
|| the string to its length before returning it.
||
|| --
|| Jon Skeet - <sk***@pobox.co m>
|| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
|| If replying to the group, please do not mail me too
|
|
Mar 1 '06 #9

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:OX******** ******@TK2MSFTN GP11.phx.gbl...
Hit send too fast.
The String reference returned from SB.ToString() is just the same
reference
to the existing String object, trimming would involve the creation of a
new
string, this would increase the memory footprint and would take a
performance hit.

Willy.
That's what I thought at first and then I thought about subsequent changes
to the builder - It must keep a reference to the string returned just in
case - subsequent mods must cause the copying of the string into a new
buffer but since this is uncommon in real code it's worth the optimisation.

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:OP******** ******@TK2MSFTN GP14.phx.gbl...
| That's why I said....
| The length of the char[] (the string backing store) in bytes is
33554432.
|
|
| Willy.
|
|
|
| "Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
| news:MP******** *************** *@msnews.micros oft.com...
|| Willy Denoyette [MVP] <wi************ *@telenet.be> wrote:
|| > No it doesn't, I really don't know where you got this value from but
| it's
|| > not the size of the SB. The Capacity and Length property values are
the
|| > number of characters the SB can hold and the actual number it holds.
| Note
|| > the "number of characters", not the number of bytes.
|| > If you create a stringbuilder like this:
|| >
|| > string s = new String('\u0306' , 12000000);
|| > StringBuilder sb = new StringBuilder(s );
|| >
|| > you will end with a SB buffer (a String) with a Capacity of 16777216
and
| a
|| > Length of 12000000.
||
|| Yes - but if you then call ToString() on that StringBuilder, you'll
|| find that the string only has a Length of 12000000 but takes up
|| 2*16777216+over head bytes. In other words, StringBuilder doesn't trim
|| the string to its length before returning it.
||
|| --
|| Jon Skeet - <sk***@pobox.co m>
|| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
|| If replying to the group, please do not mail me too
|
|

Mar 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
4719
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie, 20-30k), what are the performance differences (if any with something as trivial as this) between initializing a new instance of StringBuilder with a specified capacity vs. initializing a new instance without... (the final length is not fixed) ie,
12
7398
by: | last post by:
I know how to use a StringBuilder, which supposedly does not create a new copy of it each time you modify it contents by adding or removing text. But, I wonder how does it do that internally ? I was planning to use a stringbuilder to hold big amounts of text, with several megs o size, to be read later based on fixed offsets, so I need to know if this is suitable for it.
7
958
by: KH | last post by:
API question... Why doesn't StringBuilder have IndexOf and other similar methods like String? I can't think of a good reason off the top of my head. Easy to write helper functions to do the same thing, but sure would be helpful in certain cases. KH
33
4690
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
12
2715
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a comparative analysis between the two and, surprisingly, string seems to out perform StringBuilder by a significant amount. A string concatenation takes not quite twice as long using StringBuilder than it does with a string. This doesn't sound right to...
26
3206
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW: http://hardywang.1accesshost.com ICQ: 3359839 yours Hardy
34
3557
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical difference?
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.