Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / MSIL

String Concatenation vs. Memory Allocation in C# .NET

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
21 Dec 2011LGPL32 min read 16.8K   6
String concatenation and a very good performance analysis.

I would like to bring into your attention an old article written on this topic. This article explains a lot about string concatenation and does a very good performance analysis.

Here is a glimpse of this article:

Over the years, plenty has been written about string performance, lots of comparisons between String.Concat and StringBuilder. Today I decided to do some of my own research into the subject and contribute to the knowledge already out there. More specifically, I’ll be taking a look at the memory usage for various concatenation methods and compiler optimizations used to generate the IL.

The test scenario I defined consists of several methods, each returning the same string. The string I created is supposed to resemble a real-life scenario. I identified five different ways of concatenating strings for my test. I will be taking a look at the numbers when calling each method once and inside a very small loop of 50 calls, which is another real-life number in my case.

Single line concatenation.

The easiest way of concatenating strings together is by simply putting a plus sign between them.

C#
public string GetPlussedString()
{
 string myString = "SELECT column1,"
 + " column2,"
 + " column3,"
 + " column4,"
 + " column5,"
 + " column6,"
 + " FROM table1 t1"
 + " JOIN table2 t2"
 + " ON t1.column1 = t2.column1";
 return myString;
}

Although it seems like we are creating 9 string instances, the compiler optimizes this into the following IL:

MSIL
.method public hidebysig instance string GetPlussedString() cil managed
{
 .maxstack 1
 .locals init (
 [0] string myString)
 L_0000: ldstr "SELECT column1, column2, column3, column4, column5, 
         column6, FROM table1 t1 JOIN table2 t2 ON t1.column1 = t2.column1"
 L_0005: stloc.0
 L_0006: ldloc.0
 L_0007: ret
}

In reality, we created one string instance and returned it, which is about the most efficient way we can achieve.

When profiling the test application, I couldn’t even find a call to GetPlussedString in the profiler, which makes me believe the runtime even optimized this.

In total, our application created 113 string instances and barely used any memory.

Running this in the loop gives the following result:

Important to note is the fact that we still have 113 string instances. This is because .NET used String Interning on my string and simply returns a reference to that instance over and over.

You can read the full article here: http://www.cumps.be/nl/blog/commented/string-concatenation-vs-memory-allocation.

Thanks. Hope it helps.

Thanks to the original author for this good article.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/

Comments and Discussions

 
GeneralPlease try to understand what others are saying Pin
Hwisung Im14-May-14 19:11
Hwisung Im14-May-14 19:11 
In your example, there is no string variables in the concatinated string that needs runtime concatination. All are simple string literals. In this context, "+" simply allows you to put long string in multiple lines. Compiler simply puts them as a single string. Try your example with other string variables in the mix. You will now see different result. Looking at the IL code seems to be an interesting idea to understand what is going on under the hood.

Hope this post is updated as it can confuse new developers who does not understand the difference between string literals concatenated together vs. string variables concatenated at runtime.

Hope I could help the discussion a bit....
GeneralMy vote of 1 Pin
arvelius14-Mar-13 3:27
arvelius14-Mar-13 3:27 
GeneralMy vote of 1 Pin
Selvin24-Dec-11 4:11
Selvin24-Dec-11 4:11 
GeneralRe: My vote of 1 Pin
zenwalker198525-Dec-11 16:49
zenwalker198525-Dec-11 16:49 
Questionre Pin
pipiscrew22-Dec-11 9:03
pipiscrew22-Dec-11 9:03 
AnswerRe: re Pin
zenwalker198522-Dec-11 16:00
zenwalker198522-Dec-11 16:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.