New Message on dotNET User Group Hyd

Memory -String

Reply
  Reply to Sender   Recommend Message 3 in Discussion
From: Anand Kumar

Hi flolks,
Here is my thought on Memory-String
 

CLR maintains a hashtable (intern pool ) to  stores literals in it  lets say �Anand� , the prime reason is to conserve memory and this is shared across all app domains in the same process .The runtime add the literals into the  hashtable when a method is jited as below<o:p></o:p>

<o:p> </o:p>

static void <st1:place w:st="on">Main</st1:place>(string[] args)<o:p></o:p>

                   {<o:p></o:p>

                             string str1 ="Anand";<o:p></o:p>

                             string str2 = "Anand";<o:p></o:p>

                             if(object.ReferenceEquals(str1,str2))<o:p></o:p>

                             {<o:p></o:p>

                              Console.WriteLine("Both are share the same  memory");<o:p></o:p>

                             }<o:p></o:p>

                             Console.ReadLine();<o:p></o:p>

                   }<o:p></o:p>

<o:p> </o:p>

Remember if the string is  concatenated the it will not add to the pool as below<o:p></o:p>

<o:p> </o:p>

string str1 ="Anand";<o:p></o:p>

                             string str2 = "Kumar";<o:p></o:p>

                             String str3 = str1+str2;<o:p></o:p>

                             <o:p></o:p>

                             if(object.ReferenceEquals(str3,"AnandKumar"))<o:p></o:p>

                             {<o:p></o:p>

                                      Console.WriteLine("Both are share the same memory");<o:p></o:p>

                             }<o:p></o:p>

                             Console.ReadLine();<o:p></o:p>

Note : str3 is constructed at runtime so its returns false .<o:p></o:p>

<o:p> </o:p>

You can add the string to the pool at runtime by calling  String.Intern<o:p></o:p>

As below<o:p></o:p>

<o:p> </o:p>

static void <st1:place w:st="on">Main</st1:place>(string[] args)<o:p></o:p>

                   {<o:p></o:p>

                             string str1 ="Anand";<o:p></o:p>

                             string str2 = "Kumar";<o:p></o:p>

                             String str3 = String.Intern(str1+str2);<o:p></o:p>

                                                          <o:p></o:p>

                             if(object.ReferenceEquals(str3,"AnandKumar"))<o:p></o:p>

                             {<o:p></o:p>

                                      Console.WriteLine("Both are share the same memory");<o:p></o:p>

                             }<o:p></o:p>

                   }<o:p></o:p>

<o:p> </o:p>

Once again the main point is why this is not applicable to every type, that�s a million dollars question the answer is it applicable to immutable types such as string object.<o:p></o:p>

<o:p> </o:p>

<o:p>Cheers</o:p>

<o:p>Anand</o:p>


View other groups in this category.

Click Here!
Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to