Friday, April 15, 2011

C# String Theory—String intern pool

The string intern pool is a table that contains a single reference to each unique literal string declared or created programmatically in your application. The Common Language Runtime (CLR) uses the intern pool to minimize string storage requirements. As a result, an instance of a literal string with a particular value only exists once in the system. For example, if you assign the same literal string to several different variables, at runtime, the CLR retrieves the unique reference to that literal string from the intern pool and assigns it to each variable.
The String.Intern (string) method searches the intern pool for a string equal to the specified value. If such a string exists, its reference in the intern pool is returned. Otherwise, a reference to the specified string is added to the intern pool and that reference is returned.
In the following example, the string, declared with a value of "Intern pool" is interned; because, it is a string literal. The string built is a new string object with the same value as declared but generated by the System.Text.StringBuilder class. The Intern method searches for a string with the same value as built. Since the string already exists in the intern pool, the method returns the same reference that is assigned to declared and assigns that reference to interned.
References declared and built compare unequal because they refer to different objects, while references declared and interned compare equal because they refer to the same string.

String declared = "Intern pool"; 
String built    = new StringBuilder().Append("Intern ")
   .Append("pool").ToString(); 
String interned = String.Intern(built); 
Console.WriteLine ((Object)built==(Object)declared);    // different references
Console.WriteLine ((Object)interned==(Object)declared); // same reference 

Performance considerations

When trying to reduce the total memory allocated by your application, remember that interning has two unfortunate side effects. Firstly, the memory allocated for interned String objects is unlikely to be released until the CLR terminates: the CLR's references to interned String objects may persist after your application or application domain terminates. Secondly, to intern a string, a string must first be created. Thus, despite the fact that the memory will eventually be garbage collected, the memory used by the String object will still be allocated.

For more Details:- http://en.csharp-online.net/CSharp_String_Theory%E2%80%94String_intern_pool 

No comments:

Post a Comment