<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Łukasz Podolak &#187; CLR</title>
	<atom:link href="http://lukaszpodolak.pl/index.php/category/clr/feed/" rel="self" type="application/rss+xml" />
	<link>http://lukaszpodolak.pl</link>
	<description>Has just come up with a new idea for</description>
	<lastBuildDate>Tue, 04 Nov 2008 22:02:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Explaining the String Interning</title>
		<link>http://lukaszpodolak.pl/index.php/2008/10/07/explaining-the-string-interning/</link>
		<comments>http://lukaszpodolak.pl/index.php/2008/10/07/explaining-the-string-interning/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 11:20:04 +0000</pubDate>
		<dc:creator>lpodolak</dc:creator>
				<category><![CDATA[CLR]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://lukaszpodolak.pl/index.php/2008/10/07/explaining-the-string-interning/</guid>
		<description><![CDATA[Probably most of you have already heard about string interning,  but if not, let me introduce this concept briefly.
When you application uses string frequently, and I&#8217;m 99,9% sure it does, then think about how much memory would be wasted if the same strings that appear in your application repeatedly had been memory allocated every time [...]]]></description>
			<content:encoded><![CDATA[<p>Probably most of you have already heard about <strong>string interning,  </strong>but if not, let me introduce this concept briefly.</p>
<p>When you application uses string frequently, and I&#8217;m 99,9% sure it does, then think about how much memory would be wasted if the same strings that appear in your application repeatedly had been memory allocated every time they are used? CLR has it&#8217;s remedy for that situation. Once the AppDomain is being loaded, CLR creates an internal<strong> hashtable</strong> which keys are the actual strings and values are pointed to the objects on the managed heap. This hashtable is initially empty, and is being populated with new string entries that come while the program executes.  This means that no longer duplicated string objects are created on the heap. You may wonder, &#8220;hey, but what if I modify one instance of that &#8220;grouped&#8221; string objects? Will it affect the strings in other places that just before a while were identical to the modified one? The answer is of course <em>no</em><strong>. </strong>Remember that strings are <strong>immutable </strong>- which means that you are not allowed to modify the string instance (unless using unsafe operations). Once you <em>&#8220;modify&#8221;  </em>the string object, the new instance is created and returned under the hood and the previous instance is now ready to be GC-ed (of course the condition for this is that the <em>&#8220;old&#8221; </em>string was unique in the AppDomain).</p>
<p>When you are using the 2.0 version of the CLR (remember, please <a href="http://www.hanselman.com/blog/HowToSetAnIISApplicationOrAppPoolToUseASPNET35RatherThan20.aspx">distinct </a>the CLR versions from the .NET Framework versions) you should get the String Interning mechanism working  by default out of the box. However, there is some weirdness behind it.</p>
<p>Assemblies under CLR 2.0 are by default marked with the <strong>System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterning</strong> flag value. According to what ECMA specification says, because of that the CLR <em>may </em>choose not to intern all of the strings defined in that assembly metadata.  That would seem that we have to control the interning mechanism by using the <a href="http://msdn.microsoft.com/en-us/library/system.string.intern.aspx">Intern </a>or <a href="http://msdn.microsoft.com/en-us/library/system.string.isinterned.aspx">IsInterned</a> methods manually. Finally, let me say <u>that 2.0 version of the CLR ignores the mentioned flag</u> that is produced by the C# compiler. Pretty confusing, isn&#8217;t it? All this mess is because the concerns about performance. By default, when an assembly is loaded  CLR interns all the strings that exist in an assembly&#8217;s metadata. This process can hurt the performance significantly, that is why Microsoft choosed to &#8220;turn this feature off&#8221; by default.</p>
<p>Concluding, you can design your CLR 2.0 applications with the string interning mechanism provided by default, so this code:</p>
<p><!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset238\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;\red43\green145\blue175;\red0\green128\blue0;}??\fs20 \cf1 private\cf0  \cf1 static\cf0  \cf1 void\cf0  Main(\cf1 string\cf0 [] args)\par ??\{\par ??    \cf1 string\cf0  firstInstance = \cf4 "D'oh!"\cf0 ;\par ??    \cf1 string\cf0  stillSameInstance = \cf4 "D'oh!"\cf0 ;\par ??    \cf5 Console\cf0 .WriteLine(ReferenceEquals(firstInstance, stillSameInstance)); \cf6 // True - under CLR 2.0\par ??\cf0 \}} --></p>
<p style="background: transparent none repeat scroll 0% 0%; font-family: Courier New; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; line-height: 14px">&nbsp;</p>
<p style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args)</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">    <span style="color: blue">string</span> firstReference = <span style="color: #a31515">&#8220;D&#8217;oh!&#8221;</span>;</p>
<p style="margin: 0px">    <span style="color: blue">string</span> stillSameReference = <span style="color: #a31515">&#8220;D&#8217;oh!&#8221;</span>;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">    <span style="color: green">// True &#8211; under CLR 2.0</span></p>
<p style="margin: 0px">    <span style="color: #2b91af">Console</span>.WriteLine(ReferenceEquals(firstReference, stillSameReference)); <span style="color: green"></span></p>
<p style="margin: 0px">}</p>
<p>This code snipped should be predictable under the CLR 2.0 and print &#8220;True&#8221; to the output. However, as Jeff Richter<a href="http://www.microsoft.com/MSPress/books/6522.aspx"> in his fantastic book </a>explains,<strong> you should not rely on this mechanism</strong> and leverage string interning manually by using the appropriate methods (Intern, IsInterned) because we do not know how the next versions of the CLR will interpret the NoStringInterning flag.</p>
 <img src="http://lukaszpodolak.pl/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=5" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://lukaszpodolak.pl/index.php/2008/10/07/explaining-the-string-interning/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

