Click here to Skip to main content
11,923,013 members (61,086 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked

Strings are from earth and StringBuilder from mars.

, 2 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Here we will discuss about Strings and Stringbuffer in .NET.


I was happily married to string for a long time until I came to know the reality that “Strings are immutable” and not suitable for all scenarios. Recently I was working on a heavy HTML parser application and the program used to go out of memory frequently. The completely HTML parsing logic was using string variables.

After reading around I came to know the main reason was the immutable behavior of string. Immutable means once the data is assigned cannot be changed.

For instance if you are looping using a string variable like the code given below. Every assignment to the string creates new copies of variables and the previous copy is sent for garbage collection. So the below for loop generates different memory copies of data and the recently created is the current value.


Now you must be wondering why this absurd behavior. Any lame person (like me?) can conclude this is not efficient and neither looks logical.

The sacrifice for thread safety

Before I start with the solution I wanted to understand why Microsoft team thought about this weird behavior. Thanks to things started looking logical.

If you are using string variables in multithreaded scenarios every thread modification will create new copy of memory ensuring that you do not land in to multi-threaded issues. In other words thread safety is built-in by itself when new copies of data are created.

Not all work on ships

The next thing which started itching me is what if my application is not multi-threaded. What if my main motive is to save memory resources and ensure that I do not go out of memory issues?. Here’s comes the hero from mars “StringBuilder”.

“Stringbuilder” are not immutable, in other words if you change the variable data the same memory location is modified. VOW, that looks lot of memory saving during heavy concatenation operation as compared to string.


I wanted to see for myself that earth is flat

As a curios developer it was difficult for me to digest that internally string creates different copies of data. Out of curiosity I downloaded the CLR Profiler and ran two test of code as shown below.

One for string as the below.

string x ="";

for (inti = 0; i< 10000; i++)
x = "Shiv"+ x;


One for string builder.

StringBuilder x = newStringBuilder();

for (inti = 0; i< 10000; i++)


Watch the allocated bytes, 400235631 bytes is way greaterthan 136597bytes.

Watch the video below for the real demo

If you do not believe what I have written see the actual video demo as follows

<OBJECT type="application/x-shockwave-flash" codebase=",0,0,0" WIDTH="640" HEIGHT="360" data=""></OBJECT>


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shivprasad koirala
India India

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Rahul Rajat Singh28-Jun-12 2:12
memberRahul Rajat Singh28-Jun-12 2:12 
GeneralMy vote of 5 Pin
Farhan Ghumra17-Jun-12 20:23
memberFarhan Ghumra17-Jun-12 20:23 
GeneralMy vote of 5 Pin
Perić_Željko7-Jun-12 11:29
memberPerić_Željko7-Jun-12 11:29 
QuestionNice Pin
CIDev7-Jun-12 7:30
memberCIDev7-Jun-12 7:30 
GeneralMy vote of 4 Pin
Chamila Ranasinghe5-May-12 19:30
memberChamila Ranasinghe5-May-12 19:30 
QuestionDon't forget about... Pin
GamersWanted4-May-12 5:10
memberGamersWanted4-May-12 5:10 
First off, great article. I have been telling people for years to start using stringbuilder and stop concatenating.

Your article talks about using string and stringbuilder in a loop and I often get asked the question of why use a stringbuilder if you are not looping and only want to concatenate a couple of items together. For example:

Dim HTMLBody As String = String.Empty
HTMLBody += "User Specified Domain: " & UserDomainName & "<br/>"

I tell them there is no reason to use a StringBuilder if you have a small set of items to concatenate together, instead use a string, and use format with that string and then give them an example like this.

Dim HtmlBody as String = String.Format("User Domain: {0}
", UserDomainName) 

I do make sure to tell them that if you have a lot of items and the string is pretty long that you should use a stringbuilder instead so it's a little easier to follow.

SuggestionRe: Don't forget about... Pin
Richard Deeming10-May-12 10:01
memberRichard Deeming10-May-12 10:01 
AnswerRe: Don't forget about... Pin
DrWheetos7-Jun-12 12:12
memberDrWheetos7-Jun-12 12:12 
GeneralMy vote of 5 Pin
thatraja2-May-12 18:56
mvpthatraja2-May-12 18:56 
GeneralRe: My vote of 5 Pin
Shivprasad koirala2-May-12 19:09
memberShivprasad koirala2-May-12 19:09 
QuestionStringBuilder does allocate and discard *some* memory Pin
supercat92-May-12 10:56
membersupercat92-May-12 10:56 
AnswerRe: StringBuilder does allocate and discard *some* memory Pin
Richard Deeming10-May-12 10:09
memberRichard Deeming10-May-12 10:09 
GeneralMy vote of 5 Pin
Nicolas Gordillo2-May-12 9:43
memberNicolas Gordillo2-May-12 9:43 
GeneralMy vote of 5 Pin
Brij2-May-12 4:49
mentorBrij2-May-12 4:49 
GeneralMy vote of 5 Pin
BRShroyer2-May-12 3:06
memberBRShroyer2-May-12 3:06 
GeneralMy vote of 5 Pin
Jαved2-May-12 2:26
memberJαved2-May-12 2:26 
GeneralMy vote of 5 Pin
_ Kunal Chowdhury _2-May-12 1:48
mvp_ Kunal Chowdhury _2-May-12 1:48 
GeneralRe: My vote of 5 Pin
BRShroyer2-May-12 3:01
memberBRShroyer2-May-12 3:01 
GeneralRe: My vote of 5 Pin
_ Kunal Chowdhury _2-May-12 3:46
mvp_ Kunal Chowdhury _2-May-12 3:46 
GeneralRe: My vote of 5 Pin
Shivprasad koirala2-May-12 5:06
memberShivprasad koirala2-May-12 5:06 
GeneralRe: My vote of 5 Pin
_ Kunal Chowdhury _2-May-12 5:10
mvp_ Kunal Chowdhury _2-May-12 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.2 | Last Updated 2 May 2012
Article Copyright 2012 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid