Share via


how memory is allocated for class, class data member and member function & interface and class instance ?

Question

Monday, March 25, 2013 2:53 PM

most of the book never explain how memory is allocated for things we use.

i think no memory is allocated for class structure including member function and data member. memory is allocated when we create instance of class.....am i right ?

  1. suppose if my class structure is like below one then just tell me how memory will be allocated ?

public class Employee
        {
            double mydata = 0;
            private string _FirstName;

            public string FirstName
            {
                get { return _FirstName; }
                set { _FirstName = value ; }
            }

            private string _LastName;

            public string LastName
            {
                get { return _LastName; }
                set { _LastName = value; }
            }

            public double CalculateValue()
            {
                return mydata ;
            }
   }

  1. so when i create a instance of employee class then how memory will be allocated for property, data member and member function ?

  2. when this line will execute like Employee emp = new Employee(); then how memory will be allocated and also tell me memory will be allocated for which member of that class ?

  3. when we call emp.FirstName ="blah" then how memory will be allocated ? and how long data will be stored in memory?

  4. any memory is allocated for any interface means IStuden ?

  5. we know that for any class instance memory is allocated in heap not stack but i heared that pointer is stored in heap but actual data is stored in stack. so any one can tell me with example how data is stored in heap & stack ? and also in some cases data is only stored in stack.....why ??

  6. why one should create static class rather user can declare static method in non-static class and can access those method like

className.MethodName() ? so when and in what kind of scenario user has to create a static class ? is there any advantage of creating

static class ?

i know i have too many question. so i apologized but if possible please give answer in details. thanks

All replies (1)

Monday, March 25, 2013 3:37 PM âś…Answered | 1 vote

I'll try to answer these in order:

1, 2, and 3) When the assembly is loaded, some metadata about the type itself will be loaded into the process.  However, there is no static data, so there is no static data stored for the type.  Data is allocated when you create instances of the Employee class.  When you do this:

Employee emp = new Employee();

The "emp" variable is (normally) stored on the stack, and will be an object reference.  The Employee instance will be allocated on the heap, and consist of the object header information (type info, etc), 8 bytes for the double, and two object references for the strings (_FirstName and _LastName references, both initialized to null).  This means (in a 32bit process) you'll get 8 bytes for the double, 8 bytes for the two object references, plus 4 bytes for the syncblk (object header info), plus 4 bytes for the TypeHandle.  Depending on  padding/alignment and the type info, you may get other "empty space" to allow for proper memory alignment (though this will typically just be a couple of bytes).

4) It depends.  In general, the string reference for _FirstName will just get assigned to "point" at the instance you pass.  If you write emp.FirstName = "blah", no new memory will be allocated since "blah" is a string literal, and will have already been interned during the process startup.  If you were to build a new string, the memory for that new string would be allocated, and _FirstName would reference that memory location.  Again, this won't create new memory inside of the Employee instance, (only the new memory for the string), but just assign the reference.

5) When you define an interface, there is no memory for it (other than the metadata within the process loaded when the type is loaded).  The memory for IStudent will really just be part of the type(s) that implement the interface.

6) In general, any class instance is always stored on the heap.  The reference to the class (the variable itself) may be stored on the stack or the heap, depending on it's location.  (If it's a local within a method, it'll likely be on the stack - if it's a reference within another class, it'll be on the heap, etc).  Value types (not stored in a class instance) may be allocated on the stack entirely (though they can also be stored in registers).

7) A "static class" is just a class where all of the methods are marked static.  There is no "static class" type in the CLR itself  - the class is just marked abstract and sealed.  From a language/design standpoint, it makes your intention more clear - if your class is purely utility functions, marking it static makes it obvious that you intend for it to never be instantiated.

Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".