Tuesday 30 April 2013

String Interning


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 Strings are stored in the 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 Strings allocated in the String-Pool rather than in the General Heap. String literals (strings 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 strings 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 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.

Goto C# Experiments to access more of such content! Thanks again!

No comments:

Post a Comment