0

I implemented a something like tree in c# in this way:

public class node {
    public node parent = null;
    public node leftChild = null;     
    public node rightChild = null;
}

Now in somewhere in the code I write below

node firstNode = new node();
firstNode.rightChild = new node();
firstNode.rightChild.parent = firstNode;

my question is how much memory this code allocates? As you see there isn't any variables like integer or double. I want to know in the structures like this which we don't use pointer How we should know about the memory allocation.We are sure this is stores in the memory but there is no exact variable to aggregate all of them and say this code allocates this amount of memory. I want to know "how much memory a reference to an instance costs" in the above code

Maarten
  • 22,527
  • 3
  • 47
  • 68
  • This is not valid C# code. – CodeZombie Sep 26 '13 at 12:11
  • That's no where near syntactically correct – Mathias R. Jessen Sep 26 '13 at 12:12
  • 1
    You always have memory allocated to variables at runtime, even if it's just your platform base. On top of that, a class definition costs memory. An instance of a class costs memory. If you're asking specifically how much memory a reference to an instance costs (as in your example above), you might rephrase your question. –  Sep 26 '13 at 12:17
  • possible duplicate of [Size of A Class (object) in .NET](http://stackoverflow.com/questions/3694423/size-of-a-class-object-in-net) – Gusdor Sep 26 '13 at 12:21
  • You're looking at a platform `WORD` size per reference and the usual overhead for objects.. such as the header with type object pointer (with vtable, etc), sync block index.. really though.. why does it matter? – Simon Whitehead Sep 26 '13 at 12:22
  • As with almost every CLR/memory question: if you have to ask, you're doing it wrong. What will you do with any answer? Do you have any followup-questions you're yet keeping from us? What are you trying to do, what is your actual problem? – CodeCaster Sep 26 '13 at 12:27
  • @ZombieHunter, I changed the code you were right – Mohammad Reza Rezwani Sep 26 '13 at 12:27

2 Answers2

3

There's nothing magic about C# references, they work in much the same way as C pointers.

Your node object consists of three references - so allocating a new instance will take up 3 references worth of space on the heap.

The firstNode local variable will take up the size of one reference on the stack.

An object reference is the same size as a pointer, which is normally 4 bytes on a 32-bit CPU, and 8 bytes on a 64-bit CPU. Objects on the .Net CLR also have an 8 byte overhead, so on a 32bit system, the size of a node instance would be 8+3*4 = 20 bytes.

Ross McNab
  • 11,337
  • 3
  • 36
  • 34
  • References are on the stack.. they point to data on the heap. Semantics yes... :) EDIT: I just realised you clarified that further into your answer. My bad! – Simon Whitehead Sep 26 '13 at 12:51
1

The class certainly has variables, those three node references are fields that take space in the object. Not fundamentally different from a field of type int as far as storage goes. They are pointers at runtime, 4 bytes each in 32-bit mode, 8 bytes in 64-bit mode. Since you could only worry about object size in 32-bit mode, the object will take 8 + 3 * 4 = 20 bytes. The first 8 bytes is object overhead, any object has that.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536