17

Int has a size of 4 bytes, if I create a new Int in my program will incresse its memory consumption by 4 bytes. Right?

But if I have this class

public class Dummy{
    private int;
}

How much memory will my new class use? Will be memory consumption be lower if it was a struct? I think that the reference itself will also consume some memory.

Simon Edström
  • 6,461
  • 7
  • 32
  • 52
  • 10
    `int` is 4 bytes, 32 *bits*. A reference either takes 4 bytes on 32-bit systems or 8 bytes on 64-bit systems. A reference is a standard overhead on classes (as they are reference types), structs do not incur references and are the size of their content usually. I cannot remember if classes have any more overhead, don't think so. – Adam Houldsworth Sep 13 '12 at 09:00
  • 2
    @AdamHouldsworth, your first comment should be an answer – Habib Sep 13 '12 at 09:02
  • 1
    Nobody can explain generals of class vs struct better than Jon Skeet: http://stackoverflow.com/questions/203695/structure-vs-class-in-c-sharp – tpeczek Sep 13 '12 at 09:02
  • 1
    A class in itself doesn't consume memory, only instances of the class will consume memory. – Asciiom Sep 13 '12 at 09:03

2 Answers2

16

A single reference either takes 4 bytes on 32-bit processes or 8 bytes on 64-bit processes. A reference is a standard overhead on classes (as they are reference types). Structs do not incur references (well, ignoring any potential boxing) and are the size of their content usually. I cannot remember if classes have any more overhead, don't think so.

This question delves into class vs struct (also provided in the question comments):

Does using "new" on a struct allocate it on the heap or stack?

As stated in the comments, only instances of a class will consume this reference overhead and only when there is a reference somewhere. When there are no references, the item becomes eligible for GC - I'm not sure what the size of a class is on the heap without any references, I would presume it is the size of its content.

Really, classes don't have a true "size" that you can rely on. And most importantly this shouldn't really be the deciding factor on using classes or structs (but you tend to find guidelines stating that types at or below roughly 16 bytes can be suitable structs, and above tends towards classes). For me the deciding factor is intended usage.

When talking about structs, I feel obliged to provide the following link: Why are mutable structs “evil”?

Community
  • 1
  • 1
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • One small point: Its the process, not the system, that determines the number of bytes. A x86 process on a x64 machine will use 4 bytes, not 8, correct? – Sugrue Sep 13 '12 at 09:15
  • @Sugrue Not sure, the CLR plays a part here too... there is a 32-bit and 64-bit CLR if I remember correctly. I'll reword it slightly. – Adam Houldsworth Sep 13 '12 at 09:17
4

A class is a reference type and is located a the heap ( and will be remove there from the garbabe collector). A struct ist value type and is stored on the stack.
In case of your example Microsoft recommends a value type (struct), because a reference type causes too much overhead.

If you're interested in this topic then take a look into the book "CLR via C#" from Jeffrey Richter.

Bjoern
  • 41
  • 1
  • 2
    I think this is over simplified. [Eric Lippert's blog](http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx) covers this and the short version is value types do not always go on the stack. – psubsee2003 Sep 28 '12 at 09:25