tags:

views:

76

answers:

4

Using C#, is there any overhead in having one larger object containing a whole lot of string variables - half of which are never used in the same instance, instead of 2 objects containing half the number of string variables in which all variables will be used in all instances?

My question might actually be: what overheads are there in having large numbers of empty string variables in objects? I'm talking about 20 unused string variables.

+2  A: 

Miniscule. Don't even sweat about it.

That being said, it's most probably a bad idea from the design standpoint.

Fyodor Soikin
+6  A: 

20 unused string variables will probably waste either 80 or 160 bytes per instance, depending on whether you're on a 32 or 64-bit CLR. The value of each field would be a reference, which is why it depends on the CLR. (There may be more wastage due to padding; it depends on the details of the object.)

Whether or not this is significant will depend on how much you use these objects - obviously the more space you take, the harder the garbage collector will have to work, and the more memory your app will take. If you have a million of these objects "live" at any one point, they'll be wasting about 80 or 160MB. Is that significant? It depends on your situation.

Jon Skeet
Sure about 80 bytes for 20 strings on 32bit? Here someone said about additional 20 bytes per instance: http://www.yoda.arachsys.com/csharp/strings.html#memory
Kamarey
@Kamarey: That's talking about *instances* of strings. An unused string variable will typically hold a null reference, not a reference to a newly-created-but-empty string. (I can't tell whether you were making a joke or not, but you know I wrote that article, right?)
Jon Skeet
Yep, I know you wrote it. Just missed that talked about null references.
Kamarey
+1  A: 

Strings in .NET are interned, meaning that there would only be one instance of a unique string in the whole app. The only overhead would be a reference to that string(essentially a pointer) and it is very small to worry about. Edit As Jon commented, strings are only interned if they are a string literal ie var str = "literal";, but they can easily be interned manually if required, as shown in the MSDN link.

Don't structure your classes around potentially saving a few bytes, make your classes as readable and easy to use as possible. The runtime is pretty smart and will take care of runtime performance for you.

Igor Zevaka
You can have multiple instances of an equal string in .NET very easily. Strings are only interned if they are string *literals* or they're *explicitly* interned in code. However, in this case the space required by actual string objects is irrelevant, given that these string variables won't be used (so will have a value of `null` by default).
Jon Skeet
@Jon: they might be String.Empty
Craig Johnston
@Craig: Only if you explicitly set them that way - and in that case they'll all be referring to the same empty string, which would have been initialized anyway. In other words, there'd be no extra cost beyond what I described above.
Jon Skeet
A: 

Fyodor is right, in my opinion. The point is probably not overhead, but if having a lot of empty string is truly useful for your elaboration-flow or only a bad-design problem. In this second case you need to consider your personal overhead in maintening a so bulky code.

Christian Cabizza