tags:

views:

339

answers:

3

C#: Which uses more memory overhead? A string or a char holding a sequence of a word?

I know a char is basically an array of each char to make up a word (if more than one char), but which would ultimately cause you more memory overhead like when using a string vs. a char to hold something the length of a word?

+5  A: 

It would depend how many times you used the thing in question. The way .Net does strings is that if you had multiple strings with the same value then that value is only stored in memory once and each string variable points to that value. However each char array used would have its own memory allocation even if the contents of the char array were identical.

eg

//.Net Framework allocates memory space for "word" only once, all the three variables refer to this chunk of memory
String s1, s2, s3;
s1 = "word";
s2 = "word";
s3 = "word";

//.Net Framework allocates memory for each array
char[] c1 = new char[] { 'w','o','r','d'};
char[] c2 = new char[] { 'w','o','r','d'};
char[] c3 = new char[] { 'w','o','r','d'};
RobV
Kirtan
The remark about "same strings" (aka "interning") only applies to strings from your source code (unless you deliberately request it). So it will only use one string instance for "word" **from source code**, and one for "word2" **from source code**. But: If you build the string "word2" *not from source* (concatenation, or from user input, etc), then it will have a *different* instance - even if you do it 200 times (with 200 instances).
Marc Gravell
A: 

Generally the string is going to be more efficient as RobV pointed out multiple strings with the same value all reference the same string in memory this is called string interning. This can have a large bonus when comparing strings.

If memory is a large concern then you probably want to be careful how you deal with the strings, concatenation can become very expensive.

John Hunter
Only literal strings are interned by default.
Eric Lippert
+1  A: 

If you compare the memory storage of a string and a char[], they are quite similar. They are both allocated on the heap (except literal strings which are created at compile time). They both contain a few variables (for the length and such), and a memory area containing an array of characters.

However, depending on how the string is created, it's memory area may be larger than it's length requires, i.e. it has some unused bytes at the end of the memory area. The memory area of a char[] is always exactly as large as required by the data.

If you for example use a StringBuilder to create a string, and the capacity is larger than it's length (but not more than twice), it returns a string object which contains unused bytes at the end.

Example:

// set capacity to 8
StringBuilder s = new StringBuilder(8);
// put four characters in it
s.Append("Ouch");
// get the string
string result = s.ToString();

The result variable now points to a string object which has a memory area for the character data that is 16 bytes long, but only the first 10 are used (the four characters, plus an \x0 character at the end).

Guffa