1

If I have a class with 100 properties that are all int32's, and I have instanced 100 of these objects then does it necessarily take up 40000 bytes (plus whatever other overhead an object takes) even before any of the properties are set or does some (or all) of that space remain unallocated until you actually assign a value to the preoperties for the first time?

3
  • What's up with the downvote and no explanation... that hurts mah feelins you know. Commented Feb 11, 2012 at 3:42
  • Well, three people countered it, so you should be fine. Commented Feb 11, 2012 at 3:46
  • @Zenexer All is right in the world again :) Commented Feb 11, 2012 at 3:47

4 Answers 4

2

When an object is created, the memory for all fields is allocated immediately. Note that the size of the object also includes the object header, padding, etc.

Sign up to request clarification or add additional context in comments.

Comments

2

You use the memory as soon as you instantiate the object, because int is a value type.

Reference types work a little different. If you were to make the property strings instead of integers, you would still use the ~40,000 bytes, but no more, because at this point your strings are all null-references (null references still reserve the space for the reference). As you start setting values to the strings, then you would start using the space.

1 Comment

So am I understanding correctly that a null reference takes up 4 bytes, or is that necessarily true?
1

All class-scoped fields are "assigned" following instantiation, unlike locally-scoped variables, which can remain unassigned indefinitely. Thus, value types consume their appropriate size, and references consume the size of a moving pointer, no matter what--when they are scoped at the class level.

Note also that unless the layout is sequential (as in a struct) or explicit, most value types will be padded to at least 32 bits.

It's not always straightforward to predict how much space a null reference will consume, but were they normal pointers, they would consume 4 bytes on x86 platforms and 8 bytes on x64 platforms.

7 Comments

So you are saying if I declare 8 different bit variables in a method then it will still take up 8 bytes? I suppose that's because (on 32 bit system) the register is 32 bits and it would be slower to try to pack them together?
I'm not quite sure I understand you correctly, but if you were to have a class with two bool fields, it would consume 64 bits by default, due to padding. As a struct, it would consume 16 bits. You could also put [StructLayout(LayoutKind.Sequential)] above the class declaration to forcefully disable padding.
I.e. I meant the following line of code: "bool a, b, c, d, e, f, g, h;" would take up 16 bytes (I said 8 the first time, but meant to say 16).
Under no circumstance would that take up 16 bytes. It would take up 8 bytes without padding and 32 bytes with padding.
It would never take 64 bits, either. 32 bytes is 32 * 8 = 256 bits. A value type must consume whole bytes: thus, bool consumes one byte, or 8 bits. If you have 8 bools, you have 8 bytes, unpadded.
|
1

Int32, like all value types, has a default value. (0)

So yes; as soon as you create those Int32 variables, they are taking up memory.

1 Comment

Ah... I knew that but just forgot to make the connection that they obviously have to put that default value somewhwere :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.