Wednesday, October 30, 2013

Strings in depth C# - Immutability, string interning, String.Intern, Intern Pool and StringBuilder

a. String are immutatble

Immutability means value of a string object can't be modified once its assigned. If a string object is modified to create a different value a new string object with that new value will be created.

Because of this immutable property of strings, any operation like Addition or Deletion  will result in new string creation and it will be performance penalty. 

In cases where we repeatedly add or delete from a string, StringBuilder is suggested. Unlike string any operation on StringBuilder object will work on single instance of string object and hence will be efficient in cases where repeated string modification operations are involved.

b. String is a reference type

I know it's a simple concept yet good to note it.

c. What is string interning?

String interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.

C# also uses string interning. It interns string based on the literal value. However any operation that results in same literal value as a literal in intern pool does not get to use the existing intern reference.  

Intern Pool:

The common language runtime(CLR) automatically maintains a table, called the "intern pool", which contains a single instance of each unique literal string constant declared in a program, as well as any unique instance of String you add programmatically.
The intern pool conserves string storage. If you assign a literal string constant to several variables, each variable is set to reference the same constant in the intern pool instead of referencing several different instances of String that have identical values.
There are two methods where you can get the intern of a string i.e. get the reference for a string if it's already in the Intern pool.
a. IsInterned
Returns reference to a string if the literal string is already in the intern pool otherwise it returns null.
string s1 = "hello";
string s2 = String.IsInterned("Hello") ;
IsInterned will check intern pool for string with value "hello" and returns reference to that string.
Hence s1 will have reference as s2.
Note: IsInterned does not return bool but returns "null" when value is not present in intern pool.
b. Intern
Returns reference to a string if the literal string is already in the intern pool otherwise create a string with given literal value.
Below block is straight from MSDN about performance considerations when working with intern pool.

Performance Considerations with intern pool:

If you are trying to reduce the total amount of memory your application allocates, keep in mind that interning a string has two unwanted side effects. First, the memory allocated for interned String objects is not likely be released until the common language runtime (CLR) terminates. The reason is that the CLR's reference to the interned String object can persist after your application, or even your application domain, terminates. Second, to intern a string, you must first create the string. The memory used by the String object must still be allocated, even though the memory will eventually be garbage collected.
The .NET Framework version 2.0 introduces CompilationRelaxations.NoStringInterning enumeration member. The NoStringInterning member marks an assembly as not requiring string-literal interning. You can apply NoStringInterning to an assembly using the CompilationRelaxationsAttribute attribute. Also, when you use the Native Image Generator (Ngen.exe) to compile an assembly in advance of run time, strings are not interned across modules.

1 comment:

  1. This blog describes the basic concepts of .Net which is very useful for the interview.It also provides the different types of .Net framework.
    C#.Net Training in Noida

    ReplyDelete