views:

37

answers:

2

if the class was had a SerializableAttribute and its object was serialized with BinaryFormatter, the serialized size is equal to the size it occupied in memory?

Edit: Additional question: is there a better way to find how much space an object occupies in the memory? is this method approximate enough?

+1  A: 

Typically, serialized objects occupy more space, because explicit metadata has to be stored as well. However, in the memory space of the running process there is metadata also, so how do you account for the metadata part to make a correct comparison?

Lucero
+2  A: 

No. The members of a class/structure is stored in memory with padding to align them on an even word boundary when needed (according to respective data type). The serialised data has no such padding.

Guffa
You're right about padding, and your answer "No" is right, but the explanation is not complete at the very least, just because the name of the class, including the assembly containing the class are also stored, not to mention other stuff besides object data itself.
Igor Korkhov
@Igor: Yes, there is other information stored, but it might be possible to separate the metadata from the data. I wanted to point out that even if the metadata could be separated, it's still not a possible method to calculate the memory usage as the fundamental storage method differs.
Guffa
@Guffa: "[...] but it might be possible to separate the metadata from the data [...]" That will require to create your own BinaryFormatter :)
Igor Korkhov
Additional question: is there a better way to find how much space an object occupies in the memory? is this method approximate enough?
Jader Dias
@Jader: A popular method is to use marshalling to create an unmanaged copy of the object. That's not exact either, but it's a bit closer.
Guffa