Advertisement



< Prev
Next >



C# String intern pool






Similar to Java, one of the main goals of C# programming language is to minimize the redundancy and memory waste/overuse caused by storing String literals with duplicate values on the heap and this is why the common language runtime(CLR) reserves a special table on the Heap memory called intern pool, which stores only a single reference to each unique String literal declared in a program.






Important points to remember







How String intern pool saves memory?


Now let's understand how String intern pool saves memory by a short code :-
//String objects created by directly assigning the String literals
String s1 = "Hello";             // Statement 1 
String s2 = "Hello";             // Statement 2 

//Creating a char array
char[] arr = {'H', 'e', 'l', 'l', 'o'};

//Dynamically creating a String object from a char[] array.
String str1 = new String(arr);	// Statement 3

After executing the above piece of code, String intern pool part on heap(a special area on the heap) and the other normal part of heap are represented by Figure 1.





Note: The duplicate reference of String literal Hello stored in the reference variable, s2, will be collected by garbage collector in its next run.


Advertisement




String intern pool example


Here in the upcoming program, we have created four String objects and we are using the ReferenceEquals() static method of the Object class, which checks for equality of references i.e. if two references point to a similar String object, to show how the String intern pool saves memory does not store duplicate String objects which are created non-dynamically i.e. by directly assigning the String literals.

//C# String Intern Pool Example

using System;
using System.Text;

class A
{
public static void Main()
{
	//Creating a String object in intern pool by directly assigning a String literal value
	String s1 = "Hello";  

	//Creating a String object in intern pool by directly assigning a duplicate String literal value
	String s2 = "Hello";  
	
	//Creating a String object in intern pool by directly assigning a String literal value
	String s6 = "HelloHello";

	
	//Creating a char array
	char[] arr = {'H', 'e', 'l', 'l', 'o'};


	//Dynamically creating a String object from a char[] array.
	String s3 = new String(arr);


	//Dynamically creating a String object by calling converting a StringBuilder to String object.
	String s4 = new StringBuilder("Hello").ToString();


	//Dynamically creating a String object by programmatically adding two String objects
	String s5 = s1 + s1;


	//Calling the static method ReferenceEquals(), to check for the equality of references.
	Console.WriteLine("Do s1 and s2 point to a same String object ? "+ (ReferenceEquals(s1, s2)));
	Console.WriteLine("Do s1 and s3 point to a same String object ? "+ (ReferenceEquals(s1, s3)));
	Console.WriteLine("Do s3 and s4 point to a same String object ? "+ (ReferenceEquals(s3, s4)));
	Console.WriteLine("Do s6 and s5 point to a same String object ? "+ (ReferenceEquals(s6, s5)));
}
}


Output is :


Do s1 and s2 point to a same String object ? True
Do s1 and s3 point to a same String object ? False
Do s3 and s4 point to a same String object ? False
Do s6 and s5 point to a same String object ? False


Program Analysis






Please share this article -




< Prev
Next >
< C# String
C# Immutable String >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement