String Interning





0/5 (0 vote)
String interning
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.
- C# Experiments: String Interning (Part 1)
- C# Experiments: String Interning (Part 2)
- C# Experiments: String Interning (Part 3)
- C# Experiments: String Interning (Part 4)
The code typed-in during the demo is as follows:
//The String Pool
//This is a special Heap memory. Not like the General Heap.
//This stores UNIQUE string-variable values, ie, literals.
//The values stored in the addresses of the String Pool CANNOT BE MODIFIED.
//Immutability of Strings
//The literals in the String Pool can be freed from memory ONLY by the Garbage Collectors.
//Interning a string saves memory.
//String literals are automatically Interned; ie, Compile-time strings are auto-interned.
//Run-Time strings are NOT auto-interned. They're stored in the general heap.
//Interning has an overhead cost.
//Interning boosts comparison operation of LARGE Strings.
string s1 = "bye";
//string s2 = "bye";
//string s2 = "b" + "ye";
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");
//Intern(parameter) --> While interning a string-variable,
//specifying the string variable itself or a string-literal as a parameter,
//will do the interning.
//IsIntern(parameter) --> ["GetIntern"]
//While interning a string-variable, specifying the string variable itself
//DOES NOT do the Interning, specifying a string literal does the interning.
string str3 = new String('x', 2);
string str4 = new String('y', 2);
//string.Intern("xx");
//string.IsInterned("yy");
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!