Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net object size padding?

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...)

like image 558
Royi Namir Avatar asked May 15 '12 14:05

Royi Namir


People also ask

How do I add padding to the width of an element?

If the padding property has two values: The CSS width property specifies the width of the element's content area. The content area is the portion inside the padding, border, and margin of an element ( the box model ). So, if an element has a specified width, the padding added to that element will be added to the total width of the element.

What is the difference between padding and width in CSS?

If the padding property has four values: If the padding property has three values: If the padding property has two values: The CSS width property specifies the width of the element's content area. The content area is the portion inside the padding, border, and margin of an element ( the box model ).

What is the padding property in JavaScript?

The padding property is a shorthand property for the following individual padding properties: If the padding property has four values: If the padding property has three values: If the padding property has two values:

How much padding is added to the last block of data?

For example, if the block length is 64 bits and the last block contains only 40 bits, 24 bits of padding are added. Some encryption standards specify a particular padding scheme.


1 Answers

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.

like image 169
Andras Zoltan Avatar answered Sep 27 '22 21:09

Andras Zoltan