String Vs StringBuilder know it – in more Detail(curious)

Dear All,

We all know the difference between String  and StringBuilder. Like Once the String Object is define its length and content can not be modified. When we do concatenate the string a new object is created always.

While in StringBuilder  length and content can be modified even after object is created.It is faster than string etc.

So lets understand it what exactly happened inside this story.

for this I have created a windows application with 2 button and text box. The first button will handle String operation, second button handle String Builder operation and we show result in textbox. just simple one.

On string button I wrote following code.

private void btnString_Click(object sender, EventArgs e)
      {
          string strResult = string.Empty;
          for (int intI = 0; intI < 4000; intI++)
          {
              strResult = strResult + intI.ToString();
              strResult  = strResult + ",";
          }
          this.txtResult.Text = strResult;
      }

On string builder button  I wrote follow code.

private void btnStringBuilder_Click(object sender, EventArgs e)
{
StringBuilder strTest = new StringBuilder();
for (int intI = 0; intI < 4000; intI++)
{
strTest.Append(intI.ToString());
strTest.Append(";");

}

this.txtResult.Text = strTest.ToString();
}

 

Simple enough to understand nothing new . So now what lets start a CLR Profiler application

If you don’t have CLR Profiler application then you can download it from here.

Now start CLR Profiler Application by double click the exe

You will get following screen

CLR_EXE_STARTED

Now as we have created the windows application so we hit  “Start Application” button

As you click the Start Application button it will prompt for EXE file so  I provided the  exe of above project. as I  provided the path it started the exe of my project. So Now I clicked the string button as shown in below fig.

String_Run

As you see the output is printed  in textbox after the print I closed the “Form1”  windows as I closed it.

Wow  I got following screen

Memory_Allocation_After_String_Run

Which basically  showing the memory allocation of string type  operation.

same above step  I have repeated for String Builder  button. I open the String Operation EXE again then hit String Builder Button and then closed the “Form1” window and as I closed the window I got following screen which is memory allocation of String Builder

String_Builer_Result

Now just compare above result 

Comparision_String_VS_String_Builder

 

If you see above screen the memory allocation of string object is more than 250 times of StringBuilder object.  and there are more object to finalize in string operation but not in String builder. there is 91 Items in Generation 0 and 1 item in Generation 1 of String object while there is no item in String builder.

So from above comparison we can say that yes string object take more memory it makes GC busy a lot so possibility is higher that you may get “Out Of Memory exception” , your application is slow . So its better to use StringBuilder is such cases where you need to concatenate strings dynamically.

I hope you enjoyed the difference in detail . Will take care such situation and use proper string  or stringBuilder.

So enjoy the Weekend.

Thanks & Best Regards,

Rajat Jaiswal

Learn, Share, Grow knowledge

Advertisements

One thought on “String Vs StringBuilder know it – in more Detail(curious)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s