views:

419

answers:

6

C#: What takes up more memory? A string or bytes?

Let's say I have a line that reads "My Text", in which form would that line use up more memory, as a byte or a string?

+1  A: 

Both are pretty close. Only one real answer:

Profile it on your framework/architecture.

John Gietzen
A: 

The byte array would take less memory unless you had several copies of the string, in which case the string would take up less memory thanks to the string table.

But the real questions is, does it really matter? There are a lot of benefits you get to using the string as a string, rather than storing it as an array of bytes.

I don't know the particulars, since your question was very narrow, but I smell premature optimization.

Randolpho
+3  A: 

The byte array. This will store your text as ASCII (1 byte per character) characters, whereas a .NET string uses Unicode which are larger. However remember that .NET strings are probably more useful and in a large application the difference probably won't make a huge difference.

(note also that if you just use ASCII characters in your .NET string then the characters will still only be 1 byte each)

Elliot Hughes
Awesome first answer, dude! +1
Randolpho
Very VERY informative answer. I am very thankful for your informative response. I can't stress this enough but THANK YOU!
If you store it as a byte array, you will generally be able to store ASCII only - if you need extended UTF8 characters at all, use a string and take the memory hit, if saving a few bytes is that important.
thecoop
I don't know if this answer is entirely correct. The fact that Sytem.Char uses UTF-16 encoding does not mean that the char WILL occupy more than one byte, right? Specifically "My Text", wouldn't.
Esteban Araya
Sorry about my last comment. I didn't see the edit about ASCII.
Esteban Araya
Also remember, that an empty string requires more memory than an empty byte[]
crauscher
A: 

There's a good blog post here that gives an equation for how much space a string takes up, as well as various interactions with StringBuilder & instance allocations

thecoop
+5  A: 

It depends on the character encoding of the byte array. You can convert any string into an array of bytes, but you have to choose the encoding; there is no single standard or correct encoding. What used to be called ASCII is no use outside of the English speaking world.

In most encodings, "My Text" would be 7 bytes long. But throw in some European accented characters, or Japanese characters, and those (if they can be represented at all) may be more than one or two bytes each. In some encodings, with some text strings, the byte-array representation may be larger than the internal Unicode representation used by System.String.

Daniel Earwicker
IMO, a better answer than the accepted one, since it stresses the importance of the encoding choice.
Marc Gravell
There's no justice in the world, I tells ya.
Daniel Earwicker
A: 

Being Unicode doesn't mean that the string will take more than one byte per character, it just means it "could" take up more than one byte per character.

http://www.joelonsoftware.com/articles/Unicode.html

Robin Day