10

msdn says that

the sizeof operator can be used only in unsafe code blocks. Although you can use the Marshal.SizeOf method, the value returned by this method is not always the same as the value returned by sizeof.

and

Marshal.SizeOf returns the size after the type has been marshaled, whereas sizeof returns the size as it has been allocated by the common language runtime, including any ** padding **.

once ive read in the book : c# via clr (page 522)

that : enter image description here

questions :

1) does the padding mentioned in here :

enter image description here

is the same as mentioned in the book ?

AND

2) if i have object type of Person - how can i know its TRUE SIZE in MEMORY ?

edit - why do i need that ?

please notice this :

they have a sample of reading records :

 using (var accessor = mmf.CreateViewAccessor(offset, length))
            {

                int colorSize = Marshal.SizeOf(typeof(MyColor)); //<--------HERE
                MyColor color;


                for (long i = 0; i < length; i += colorSize)
                {
                    accessor.Read(i, out color);
                    color.Brighten(10);
                    accessor.Write(i, ref color);
                }
            }
        }

if the size being reported by MARSHAL.sizeOF is not the size as sizeOF so - which should i choose ? it has to be accurate !!

according to this sample , they dont consider the padding , and they should... ( or not...)

1
  • becuase if im using memorymappedfile - and have a file with many records (of person), i need to know the offset length
    – Royi Namir
    May 15, 2012 at 14:04

1 Answer 1

5

This might seem disingenuous - but the size you're interested from the memory-mapped-file point of view is not the same as the size of that object in memory. They might be called memory-mapped files, but in .Net that doesn't necessarily mean quite the same thing as it would in native code. (The underlying implementation is still the same, though - a section of logical memory is mapped to a section of a file, so the name is still correct)

sizeof returns the correct size of the object in physical memory, including any padding bytes etc. Therefore if you need to know the exact size of an object in native-memory terms, use that (but this doesn't apply to memory-mapped files as I'll explain in a moment).

As the documentation says, Marshal.SizeOf reports the size of an object from the .Net point of view, excluding the two hidden data items; which are used by the runtime only.

The example you have copied uses Marshal.SizeOf because the padding values are related only to the physical object when in memory. When the object is serialized, only the logical .Net data is serialized. When the object is loaded back again, those two padded values are re-assigned based on the state of the runtime at that point. E.g. The type pointer might be different. It would be meaningless to serialize them. It would be like serializing a native pointer (not an offset) to disk - it's incredibly unlikely that the data it points to will be in the same place next time.

Ergo - if you want to know how much an array of 100 Color objects uses in physical memory - use sizeof; if you want to know how large the memroy-mapped file for the same data would be, use Marshal.SizeOf.

7
  • so why did they use Marshal.SizeOf(typeof(MyColor)); and not sizeof when they read in the memoryMappedFile ?
    – Royi Namir
    May 15, 2012 at 14:21
  • Updated my answer. Think of the .Net object you know and love as logical data; with the actual memory the physical. In general you never need the physical; even when using memory-mapped files - because in .Net you're dealing with a file mapped to logical data, not physical memory. Yes, the underlying implementation is truly using memory-mapped files; but .Net is then using that to seed other objects. May 15, 2012 at 14:27
  • what about the padding issue from the book and from msdn - is it the same ?
    – Royi Namir
    May 15, 2012 at 14:44
  • I believe so - I don't know of any other padding the .Net runtime does. The only possible thing could be word-boundary aligment, but that relates to the start of an object's location in memory, so I doubt that applies. May 15, 2012 at 14:47
  • so- becuase im accesing via straight memory - i DO NEED THE size+padding - hence i use the marshal.sizeOf ?
    – Royi Namir
    May 15, 2012 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.