none
why they say struct takes lesser memory than class objects? RRS feed

  • Question

  • hi friends,

    is known that struct objects consume less memory than class objects because struct object does not need an additional memory location to store the memory address of the object created from the new operator.

    To my understanding-

    when I create an instance of a struct using new operator, it returns a copy of the original object, which means, now there are two objects the original object the copy.. so if the original object size is 1K, instantiation of that struct takes 2K (original + copy)

    When I create an instance of a class, new operator return the memory address of the original object that was created. So assume original object size is 1K and to keep the memory address it take 5bytes the total object creation of that class takes less than 2K (original + memory size to store the memory address)

    So why they say a struct object consumes less memory (less resources) than a class object?

    thanks


    I use Visual studio 2012 Ultimate and SQL server 2008 developer edition!

    Tuesday, November 12, 2013 4:48 AM

Answers

  • "is known that struct objects consume less memory than class objects because struct object does not need an additional memory location to store the memory address of the object created from the new operator."

    Yes, that's indeed a difference. For example if you have an array of 10 System.Drawing.Point values then the amount of memory used for those values is 10 * sizeof(Point) = 10 * 8 = 80. On the other hand if Point was a reference type then the array would store references so the total amount of memory needed would have been 10 * (sizeof(Point) + sizeof(reference)) = 10 * (16 + 4) = 200 (on x86).

    Now, if you follow my computations carefully you'll perhaps notice that sizeof(Point) is different in those 2 cases. In the value type case it is 8 - the size of 2 Int32 fields it contains. In the reference type case the size is 16 - the size of 2 Int32 fields (8 bytes like before) + the size of the method table pointer (4 bytes on x86) + the size of the object pre header (4 bytes on x86). So it's not only that references require memory but reference type instance use more memory due to additional content that's no present in value types. And it is this particular difference that is usually used as the basis for the "structs use less memory" statement.

    "now there are two objects the original object the copy"

    Well, sort of. Since value types are always copied then in theory you use more memory for all those copies. But that's rarely a concern. For one thing value types are (should be) small, less than 16-32 bytes for example, that minimizes the impact of copying. And most importantly, value types are usually used exactly because of this copy by value semantics, there's no point in debating the memory usage when you actually need those copies.

    @Adavesh: "when you create a structure instance, then that instance is actually stored on stack."

    Ah, the never ending legend of value types being stored on stack. And if I declare a field of type System.Drawing.Point in a class then can you tell me where that value will be stored?

    • Proposed as answer by Sam Vishwas Tuesday, November 12, 2013 4:57 PM
    • Marked as answer by Eason_H Wednesday, November 20, 2013 2:23 AM
    Tuesday, November 12, 2013 6:53 AM
    Moderator

All replies

  • http://msdn.microsoft.com/en-us/library/vstudio/ms173109.aspx

    As you can see in the above article structures and classes are like a blue print to create objects or instances at the run time. If you have exact matching definition for a class and a structure then instance of that class and that structure is expected to occupy the exact same memory space. Class instance is reference type and the reference will need little extra memory.

    However you will need exact same amount memory of the original instance of a structure every time you make a copy of it. For example 5 copies of your structure will need 5K.

    On the other hand five references of an instance of a class will need 1K + 5 * size of reference (Generally Size of reference is 32 or 64 bits based on your OS).

    So in short it is not necessary that the structures are space savers.

    • Proposed as answer by Sam Vishwas Tuesday, November 12, 2013 5:23 AM
    Tuesday, November 12, 2013 5:23 AM
  • when you create a structure instance, then that instance is actually stored on stack. But a class instance is always created on heap.

    You should know that, any objects (class objects) that are created on heap require the garbage collection to occur to clean them. Garbage collection always incurs a little delay in execution, requires additional resources (like threads). On the other hand, structure objects are stored on stack, which do not need any Garbage collection, so require less resources.

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, November 12, 2013 6:05 AM
  • "is known that struct objects consume less memory than class objects because struct object does not need an additional memory location to store the memory address of the object created from the new operator."

    Yes, that's indeed a difference. For example if you have an array of 10 System.Drawing.Point values then the amount of memory used for those values is 10 * sizeof(Point) = 10 * 8 = 80. On the other hand if Point was a reference type then the array would store references so the total amount of memory needed would have been 10 * (sizeof(Point) + sizeof(reference)) = 10 * (16 + 4) = 200 (on x86).

    Now, if you follow my computations carefully you'll perhaps notice that sizeof(Point) is different in those 2 cases. In the value type case it is 8 - the size of 2 Int32 fields it contains. In the reference type case the size is 16 - the size of 2 Int32 fields (8 bytes like before) + the size of the method table pointer (4 bytes on x86) + the size of the object pre header (4 bytes on x86). So it's not only that references require memory but reference type instance use more memory due to additional content that's no present in value types. And it is this particular difference that is usually used as the basis for the "structs use less memory" statement.

    "now there are two objects the original object the copy"

    Well, sort of. Since value types are always copied then in theory you use more memory for all those copies. But that's rarely a concern. For one thing value types are (should be) small, less than 16-32 bytes for example, that minimizes the impact of copying. And most importantly, value types are usually used exactly because of this copy by value semantics, there's no point in debating the memory usage when you actually need those copies.

    @Adavesh: "when you create a structure instance, then that instance is actually stored on stack."

    Ah, the never ending legend of value types being stored on stack. And if I declare a field of type System.Drawing.Point in a class then can you tell me where that value will be stored?

    • Proposed as answer by Sam Vishwas Tuesday, November 12, 2013 4:57 PM
    • Marked as answer by Eason_H Wednesday, November 20, 2013 2:23 AM
    Tuesday, November 12, 2013 6:53 AM
    Moderator
  • :) yes Mike. You are right.

    And of course the point object you mentioned will be stored on heap. But, OP's previous questions I felt that he is quite new to C#. So, going deeper into Value types may not be necessary for him. And, once he passes beginners level, he can always go through Eric Lippert's great articles on Values types .. right?  


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, November 12, 2013 7:10 AM