Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine size of an object, c#?

Tags:

c#

I have the following class:

public class MyClass
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public double Amount { get; set; }
    } 

When I try to find out size of this class on a 64 bit system by using WinDbg I get the size 40 which I am not able to understand, as far as I have read MyClass should have

8 bytes for SyncBlock
8 bytes for TypeHandle
8 bytes for string reference
4 bytes for Int32
8 bytes for double

= 36 bytes

I don't have 10 reputation that's why i am not able to post image. Anyone has any idea why WinDbg is showing 4 extra bytes ?

like image 511
Kush Avatar asked Nov 19 '13 23:11

Kush


1 Answers

I believe what you're seeing is the effect of things needing to align to 8 byte boundaries in 64 bit builds (and 4 byte boundaries in 32 bit builds). 40 is the closest size >= 36 that is on an 8 byte boundary. These links talk about object size:

Of Memory and strings (Jon Skeet's blog)

Benchmarking C# Struct and Object Sizes

Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects

How Much Memory Does a C# String Take Up

like image 55
hatchet - done with SOverflow Avatar answered Oct 09 '22 21:10

hatchet - done with SOverflow