Meeting the needs of your business from a distance

String.Intern

by Mark Shiffer 11/28/2007 12:26:00 PM

I just happened to run across this and found it interesting. I am just documenting it here for my future reference. This is straight from the MS docs on the subject: 

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, 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 variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

The Intern method uses the intern pool to search for a string equal to the value of str. If such a string exists, its reference in the intern pool is returned. If the string does not exist, a reference to str is added to the intern pool, then that reference is returned.

In the C# example that follows, the string, s1, which has a value of "MyTest", is already interned because it is a literal in the program.

The System.Text.StringBuilder class generates a new string object that has the same value as s1. A reference to that string is assigned to s2.

The Intern method searches for a string that has the same value as s2. Since such a string exists, the method returns the same reference that is assigned to s1, then that reference is assigned to s3.

References s1 and s2 compare unequal because they refer to different objects, while references s1 and s3 compare equal because they refer to the same string.

 String s1 = "MyTest";
 String s2 = new StringBuilder().Append("My").Append("Test").ToString();
 String s3 = String.Intern(s2);
 Console.WriteLine((Object)s2==(Object)s1); // Different references.
 Console.WriteLine((Object)s3==(Object)s1); // The same reference.
 

Compare this method to the IsInterned method.

Version Considerations
Starting with the .NET Framework version 2.0, there is a behavioral change in the Intern method. In the following C# code sequence, the variable str1 is assigned a reference to Empty, the variable str2 is assigned the reference to Empty that is returned by the Intern method, then the references contained in str1 and str2 are compared for equality.

string str1 = String.Empty;
string str2 = String.Intern(String.Empty);
if ((object) str1) == ((object) str2) …
 

In the .NET Framework version 1.1, str1 and str2 are not equal, but starting in the .NET Framework version 2.0, str1 and str2 are equal.

Performance Considerations
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 languange 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.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Research

Related posts

Comments

November 28. 2007 16:08

Sam

I like to put string literals in a resources file and call GetString() on that. I assume the compiler puts the names of the resources in the Intern pool, but I don't know what it does with the actual resources. I guess they are in an XML file.

I have never used String.Intern to any advantage but I can see certain situations where it might be useful--such as an application that somehow constructs a billion strings but those strings all end up being one of a few different ones.

Sam

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

July 9. 2008 00:30

About the author

Name of author Mark Shiffer
CEO & CIO of MS Consulting

E-mail me Send mail

Calendar

<<  July 2008  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Pages

    Recent posts

    Recent comments

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2008

    Sign in

    Copyright © 2001-2008 MS Consulting, Inc. All Rights Reserved.