Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are String and Char types stored in memory in .NET?

I'd need to store a language code string, such as "en", which will always contains 2 characters.

Is it better to define the type as "String" or "Char"?

private string languageCode;

vs

private char[] languageCode;

Or is there another, better option?

How are these 2 stored in memory? how many bytes or bits for will be allocated to them when values assigned?

like image 853
The Light Avatar asked May 28 '12 09:05

The Light


People also ask

Does char store a string?

You can store a string in a char array of length 1, but it has to be empty: char a[1] = ""; The terminating null character is the only thing that can be stored.

How is an array of string represented in the memory?

In C++, the string can be represented as an array of characters or using string class that is supported by C++. Each string or array element is terminated by a null character. Representing strings using a character array is directly taken from the 'C' language as there is no string type in C.

How much storage does a string have?

An empty String takes 40 bytes—enough memory to fit 20 Java characters. The results clearly show that a String 's memory growth tracks its internal char array's growth. However, the String class adds another 24 bytes of overhead.


2 Answers

How They Are Stored

Both the string and the char[] are stored on the heap - so storage is the same. Internally I would assume a string simply is a cover for char[] with lots of extra code to make it useful for you.

Also if you have lots of repeating strings, you can make use of Interning to reduce the memory footprint of those strings.

The Better Option

I would favour string - it is immediately more apparent what the data type is and how you intend to use it. People are also more accustomed to using strings so maintainability won't suffer. You will also benefit greatly from all the boilerplate code that has been done for you. Microsoft have also put a lot of effort in to make sure the string type is not a performance hog.

The Allocation Size

I have no idea how much is allocated, I believe strings are quite efficient in that they only allocate enough to store the Unicode characters - as they are immutable it is safe to do this. Arrays also cannot be resized without allocating the space in a new array, so I'd again assume they grab only what they need.

Overhead of a .NET array?

Alternatives

Based on your information that there are only 20 language codes and performance is key, you could declare your own enum in order to reduce the size required to represent the codes:

enum LanguageCode : byte
{
    en = 0,
}

This will only take 1 byte as opposed to 4+ for two char (in an array), but it does limit the range of available LanguageCode values to the range of byte - which is more than big enough for 20 items.

You can see the size of value types using the sizeof() operator: sizeof(LanguageCode). Enums are nothing but the underlying type under the hood, they default to int, but as you can see in my code sample you can change that by "inheriting" a new type.

like image 73
Adam Houldsworth Avatar answered Sep 19 '22 12:09

Adam Houldsworth


Short answer: Use string

Long answer:

private string languageCode;

AFAIK strings are stored as a length prefixed array of chars. A String object is instantiated on the heap to maintain this raw array. But a String object is much more than a simple array it enables basic string operations like comparison, concatenation, substring extraction, search etc

While

private char[] languageCode;

will be stored as an Array of chars i.e. an Array object will be created on the heap and then it will be used to manage your characters. But it still has a length attribute which is stored internally so there are no apparent savings in memory when compared to a string. Though presumably an Array is simpler than a String and may have fewer internal variables thus offering a lower memory foot print (this needs to be verified).

But OTOH you loose the ability to perform string operations on this char array. Even operations like string comparison become cumbersome now. So long story short use a string!

like image 32
Autodidact Avatar answered Sep 22 '22 12:09

Autodidact