How are String and Char types stored in memory in

2019-01-18 23:12发布

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?

5条回答
男人必须洒脱
2楼-- · 2019-01-18 23:52

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!

查看更多
戒情不戒烟
3楼-- · 2019-01-19 00:07

If you want to store exactly 2 chars, and do it most efficiently, use a struct:

struct Char2
{
 public char C1, C2;
}

Using this struct will generally not cause new heap allocations. It will just upsize an existing object (by the minimum possible amount) or consume stack space which is very cheap.

查看更多
不美不萌又怎样
4楼-- · 2019-01-19 00:08

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

Every instance in .NET is stored as follows: one IntPtr-sized field for the type identifier; one more for locking on the instance; the remainder is instance field data rounded up to an IntPtr-sized amount. Hence, on a 32-bit platform every instance occupies 8 bytes + field data.

This applies to both a string and a char[]. Both of these also store the length of the data as an IntPtr-sized integer, followed by the actual data. Thus, a two-character string and a two-character char[], on a 32-bit platform, will occupy 8+4+4 = 16 bytes.

The only way to reduce this when storing exactly two characters is to store the actual characters, or a struct containing the characters, in a field or an array. All of these would consume only 4 bytes for the characters:

// Option 1
class MyClass
{
    char Char1, Char2;
}

// Option 2
class MyClass
{
    CharStruct chars;
}
...
struct CharStruct { public char Char1; public char Char2; }

MyClass will end up using 8 bytes (on a 32-bit machine) per instance plus the 4 bytes for the chars.

// Option 3
class MyClass
{
    CharStruct[] chars;
}

This will use 8 bytes for the MyClass overhead, plus 4 bytes for the chars reference, plus 12 bytes for the array's overhead, plus 4 bytes per CharStruct in the array.

查看更多
forever°为你锁心
5楼-- · 2019-01-19 00:14

Strings indeed have a size overhead of one pointer length, i.e. 4 bytes for a 32 bit process, 8 bytes for a 64 bit process. But then again, strings offer so much more in return than char arrays.

If your application uses many short strings and you don't need to use their string properties and methods that often, you could probably safe a few bytes of memory. But if you want to use any of them as a string, you will first have to create a new string instance. I can't see how this will help you safe enough memory to be worth the trouble.

查看更多
家丑人穷心不美
6楼-- · 2019-01-19 00:17

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.

查看更多
登录 后发表回答