String interning in C#

How interning reuses memory for strings of same value

Home DailyDrop

Daily Knowledge Drop

String interning is a process whereby different string variables of the same value, can both point to the same memory location (to improve memory usage). This is done automatically in most cases, but it is also possible to force this process when not automatically done.

Below we'll look at some cases when interning is done automatically, and how to force it when not automatically done.


Automatic interning

First let's look at how string interning happens automatically.

When working with two string literals:

var helloWorld = "hello world";
var helloWorld2 = "hello world";

Console.WriteLine(Object.ReferenceEquals(helloWorld, helloWorld2));

In this example, two strings are declared, both having the same value - a check is then performed compare the references (memory) of the two variables.

The output:

    True

The same occurs when appending string literals:

var helloWorld = "hello world";
var helloWorld3 = "hello " + "world";

Console.WriteLine(Object.ReferenceEquals(helloWorld, helloWorld3));

The output again indicates they point to the same memory:

    True

Now lets look at an example where two variables are set, and then combined:

var helloWorld = "hello world";
var hello = "hello";
var world = "world";
var helloWorld4 = hello + " " + world;

Console.WriteLine(Object.ReferenceEquals(helloWorld, helloWorld4));

The output now changes: even though helloWorld and helloWorld4 have the same string value, they no longer point to the same memory address.

    False

Manual interning

The interning process can manually be triggered when its unable to automatically be determined, using the String.Intern method.

The Intern method will use an internal intern pool to search for a string equal to the specified value - if such a string exists, that referent to the intern pool is returned, otherwise a reference to the specified string is added to the intern pool, and then returned.

var helloWorld = "hello world";
var hello = "hello";
var world = "world";
var helloWorld5 = string.Intern(hello + " " + world);

Console.WriteLine(Object.ReferenceEquals(helloWorld, helloWorld45);

This is the same as the previous example, except now the String.Intern method is used. The output now changes to:

    True

As mentioned above, if the string specified to the Intern method doesn't already exists, a new reference will be returned, and no error or exceptions will occur:

var helloWorld = "hello world";
var hello = "hello";
var world = "world";
var helloWorld6 = string.Intern(hello + "12345" + world);

Console.WriteLine(Object.ReferenceEquals(helloWorld, helloWorld6));

helloWorld and helloWorld6 definitely have two very different values now, and the memory location of each reflects that:

    False

Notes

Interning is an internal implementation and generally not something which should be worried about. Manually using interning however can be used to improve performance in some very niche cases where micro-optimization is required.


References

String.Intern(String) Method

Daily Drop 58: 22-04-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net string interning