Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
hi all

I want to calculate size of memory used in my Node class of BST:
here is Node class Code:
C#
class Node
    {
        /// <summary>
        /// class Node for Nodes of BST
        /// *** Word -> Hash() -> key
        /// </summary>
        public int key{ get; set; } // Hashed Word goes here
        public string word{ get; set; }// word in English
        public string translate{ get; set; }// Translate in Peresian
        public int count{ get; set; } // Count nodes  when Inserted to BST
        public Node right{ get; set; } // A pointer to right node of current node
        public Node left{ get; set; } // A pointer to left node of current node
        public Node parent { get; set; } // A pointer to Parent of current node


my problem is pointers(right,left,parent) ,because finding sizeof(int) and sizeof(int) is very simple.How I calculate it?
thank you.
Posted
Comments
Mehdi Gholam 21-Jun-14 5:20am    
Why? memory used is only important when you have a limit.
Mohammad Sharify 21-Jun-14 6:40am    
yes,right...but this is my final data structure project.

1.Basically you should use sizeof() and/or Marshal.SizeOf() to calculate it.

2.The size of the right, left and parent properties, becuase they are reference to other Node objects should be 4 (for Win32 application) and 8 (for Win64 application).

3.You could use memory profiler tools to inspect and optimize the memory allocation, like the next one: http://memprofiler.com/[^]
 
Share this answer
 
First off, those aren't pointers, they are references. Pointers are a very different beastie and require unsafe code to use.
As does sizeof - because you don't need to know under normal circumstances.
However, since these are references, they have a fixed size - exactly the same as that for a string. The actual size is fixed, but what it is depends on your system: for 32 bit systems, it is 32 bits, for 64 bits systems it is 64.

However, this is the size of the reference not the heap object that is being referred to:
C#
string s1 = "Hello";
string s2 = "However, since these are references, they have a fixed size - exactly the same as that for a <code>string</code>.";
s1 and s2 are the same size - 32 or 64 bits - because the variable is not the object it is referring to.

So...for a 32 bits system, your Node is 28 bytes, for a 64 bit system it's 48 bytes.
But...it may not be, depending on how the framework organised things when it creates the object: it may add padding to extend the integers to 64 bits, or pack them together into a 64 bit word, and it will quite probably add padding at the end to "fill" a natural size on the heap at 32 bytes or 64 bytes.

It's not really relevant, unless you are trying to use a struct instead of a class - because you really don't have much control over the actual space it takes up! :laugh:
 
Share this answer
 
Comments
Mohammad Sharify 21-Jun-14 6:39am    
tnx :) . But I have Persian(like Arabic) chars in "string translate",are they same with English size?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900