Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interning in .Net Framework - What are the benefits and when to use interning

I want to know the process and internals of string interning specific to .Net framework. Would also like to know the benefits of using interning and the scenarios/situations where we should use string interning to improve the performance. Though I have studied interning from the Jeffery Richter's CLR book but I am still confused and would like to know it in more detail.

[Editing] to ask a specific question with a sample code as below:

private void MethodA() {     string s = "String"; // line 1 - interned literal as explained in the answer              //s.intern(); // line 2 - what would happen in line 3 if we uncomment this line, will it make any difference? }  private bool MethodB(string compareThis) {     if (compareThis == "String") // line 3 - will this line use interning (with and without uncommenting line 2 above)?     {         return true;     }     return false; } 
like image 896
VSS Avatar asked Nov 08 '11 17:11

VSS


People also ask

What is String intern () When and why should it be used?

String Interning is a method of storing only one copy of each distinct String Value, which must be immutable. By applying String. intern() on a couple of strings will ensure that all strings having the same contents share the same memory.

What is string interning in C#?

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.

Why do we use interns?

The intern() method is used to store strings in the String constant pool and is majorly used if we want the strings present in the heap to be stored in the String constant pool. A string pool is similar to the way objects are allocated. It is empty by default and it is a part of the Java String class.


2 Answers

In general, interning is something that just happens, automatically, when you use literal string values. Interning provides the benefit of only having one copy of the literal in memory, no matter how often it's used.

That being said, it's rare that there is a reason to intern your own strings that are generated at runtime, or ever even think about string interning for normal development.

There are potentially some benefits if you're going to be doing a lot of work with comparisons of potentially identical runtime generated strings (as interning can speed up comparisons via ReferenceEquals). However, this is a highly specialized usage, and would require a fair amount of profiling and testing, and wouldn't be an optimization I'd consider unless there was a measured problem in place.

like image 115
Reed Copsey Avatar answered Oct 07 '22 04:10

Reed Copsey


Interning is an internal implementation detail. Unlike boxing, I do not think there is any benefit in knowing more than what you have read in Richter's book.

Micro-optimisation benefits of interning strings manually are minimal hence is generally not recommended.

This probably describes it:

class Program {     const string SomeString = "Some String"; // gets interned      static void Main(string[] args)     {         var s1 = SomeString; // use interned string         var s2 = SomeString; // use interned string         var s = "String";         var s3 = "Some " + s; // no interning           Console.WriteLine(s1 == s2); // uses interning comparison         Console.WriteLine(s1 == s3); // do NOT use interning comparison     } } 
like image 22
Aliostad Avatar answered Oct 07 '22 05:10

Aliostad