Introduction
The most used (and misused) data-type in most applications is the String
! Their properties let us leverage on many aspects of coding.
Here, we’ll discuss about the way String
s are stored in memory and how they can be allocated efficiently!
The following write-up describes all the concepts covered during the video-demo.
We start by discussing about the special Heap memory (the String
Pool), that stores Unique String
Literals. The way in which the occupied memory is de-allocated by a Garbage Collector is an implementation detail.
Interning is the property of having String
s allocated in the String
-Pool rather than in the General Heap. String
literals (string
s declared before Compilation-Time) are auto-interned; whereas String
-Variables that are assigned values during Run-Time aren’t automatically interned – We can force an Intern on them though!
Interning has its pros and cons. It’s best to use this concept under scenarios where we have millions of string
s that have many copies of themselves (there is more to it than this!). We wind up by discussing the difference between the Intern()
and the IsInterned()
functionality.
For a deeper understanding on all that has been mentioned above, please check out the 4 video links given below where I go through the demo in full detail.
The code typed-in during the demo is as follows:
string s1 = "bye";
string s2 = string.Copy(s1);
s2 = string.Intern(s2);
if (string.ReferenceEquals(s1, s2))
MessageBox.Show("Same Address");
else
MessageBox.Show("Different Address");
string str1 = "aa";
string str2 = new String('a', 2);
str2 = string.Intern("aa");
if (string.ReferenceEquals(str1, str2))
MessageBox.Show("Same Address");
else
MessageBox.Show("Different Address");
string str3 = new String('x', 2);
string str4 = new String('y', 2);
string.Intern(str3);
string.IsInterned(str4);
MessageBox.Show(string.IsInterned(str3) != null ? "str3 is Interned" : "str3 is not Interned");
MessageBox.Show(string.IsInterned(str4) != null ? "str4 is Interned" : "str4 is not Interned");
Thank you for reading this post and watching the videos. Please subscribe, comment, and rate the channel if you liked the videos.
Go to C# Experiments to access more of such content! Thanks again!
CodeProject