Saturday, December 3, 2011

String Interning or why you shouldn’t use string in a lock statement.

 

Hi All,

Today I want to talk about a nice and relatively unmentioned feature of the .Net framework – string interning. I will start with a small riddle. What do you think is the output of the following application?

  1. class Program
  2. {
  3.   static void Main(string[] args)
  4.   {
  5.     Task t0 = new Task(() => { PrintNumber(0); });
  6.     Task t1 = new Task(() => { PrintNumber(1); });
  7.     t0.Start();
  8.     t1.Start();
  9.     Task.WaitAll(t0, t1);
  10.   }
  11.  
  12.   public static void PrintNumber(int number)
  13.   {
  14.     lock ("Monkey")
  15.     {
  16.       for (int i = 0; i < 10; i++)
  17.       {
  18.         Console.Out.Write(number);
  19.         Thread.Sleep(200);
  20.       }
  21.     }
  22.   }
  23. }

If you said a series of 0s and then a series of 1s (or vise versa) then you are right. But why? When we lock the string “Monkey” shouldn’t the compiler create two instances of the this string (one per call)? This string is a local variable of the method.

As you probably know the String class in .Net is immutable which means that once an instance of this class is created it cannot be edited in any way. Any operation that changes the content of the string will always create a new instance of the string, this instance reflects the changes done on the original string. This behavior is not cemented using some sort of a language directive and is more of a “word of mouth”, an agreements if you wish, between the framework and the coders. Nevertheless, this immutability allows the .Net framework to treat the string in a special way. If the compiler can infer the string content at compile time it will not be allocated on the local heap. It will be allocated on the System Appdomain and added to an interning hash table on the System Domain (called the intern pool). Every time a string is created the framework will search the intern pool to check if an instance of this string already exists and if so it will return a reference to that instance. Note that this would happen automatically only to strings which the compiler can infer during compilation but you can force a string to enter the intern pool by using the String.Intern method.

Here is a simple program to list some interesting cases of string interning:

  1. static void Main(string[] args)
  2.     {
  3.       string s1 = "Hello World";
  4.       string s2 = "Hello World";
  5.       string s3 = s2;
  6.       StringBuilder sb1 = new StringBuilder(s1);
  7.       string s4 = sb1.ToString();
  8.       string s5 = string.Intern(s4);
  9.       string s6 = s1.Clone() as string;
  10.       
  11.  
  12.       Console.Out.WriteLine(String.Format("s1 == s2 - {0}", s1 == s2));
  13.       Console.Out.WriteLine(String.Format("Object.ReferenceEquals(s1,s2) - {0}", Object.ReferenceEquals(s1, s2)));
  14.       Console.Out.WriteLine(String.Format("Object.ReferenceEquals(s1,s3) - {0}", Object.ReferenceEquals(s1, s3)));
  15.       Console.Out.WriteLine(String.Format("Object.ReferenceEquals(s1,s4) - {0}", Object.ReferenceEquals(s1, s4)));
  16.       Console.Out.WriteLine(String.Format("Object.ReferenceEquals(s1,s5) - {0}", Object.ReferenceEquals(s1, s5)));
  17.       Console.Out.WriteLine(String.Format("Object.ReferenceEquals(s1,s6) - {0}", Object.ReferenceEquals(s1, s6)));
  18.  
  19.       StringBuilder sb2 = new StringBuilder();
  20.       for (int i = 0; i < 20000; i++)
  21.         sb1.Append("a");
  22.  
  23.       string[] strings = new string[2];
  24.       for (int i = 0; i < 2; i++)
  25.       {
  26.         strings[i] = String.Intern(sb1.ToString());
  27.       }
  28.  
  29.       
  30.       Console.Out.WriteLine(String.Format("(s1,s6) - {0}", Object.ReferenceEquals(strings[0],strings[1])));
  31.  
  32.     }

And the output is:

s1 == s2 - True
Object.ReferenceEquals(s1,s2) - True
Object.ReferenceEquals(s1,s3) - True
Object.ReferenceEquals(s1,s4) - False
Object.ReferenceEquals(s1,s5) - True
Object.ReferenceEquals(s1,s6) - True
(s1,s6) – True

Couple of thigs to notice:

  1. String operations that do not change the string (such as Clone or ToString) will return the same instance.
  2. If an interend string is allocated on the System Appdomain it will never be released (Only when the CLR is terminated)
  3. StringBuilder will always generate a new instance of a string (the compiler can’t infer what the StringBuilder contains).

I am not sure where interning can actually help (although I heard that there are some use cases in ASP.NET where this behavior is benefitial) but you should be aware that it exist and never use a string in a lock statement. Who knows where this string comes from Smile

This is what MSDN has to say about the lock statement :

lock("myLock") is a problem because any other code in the process using the same string, will share the same lock.

I hope now you understand why.

 

I want to thank Ilya Kosaev for helping me with this blog post.

Thanks for reading,

Boris.

3 comments:

  1. Very interesting. The funny thing is that it works practically the same way in Java (at least in Sun JVM).

    ReplyDelete
  2. Is there any reason not to use a designated "new object()" for locking?

    ReplyDelete
  3. Yes you are right.
    If you have a scope for all your lock statements then you should create an object which will be dedicated for all the locks. If you don't have a scope (or need something more sophisticated than a critical section) then you would have to resort to Windows locking mechanisms.

    ReplyDelete