Memory size of Boolean

I'm memory profiling an application and as I was scrolling through the data I noticed that System.Boolean objects are showing a size of 12 bytes. I thought, though, the size of a boolean in C# was 1 byte (http://msdn.microsoft.com/en-us/library/eahchzkf.aspx)

So what is causing the extra 11 bytes to show up in Memory Profiler?

Thanks

Comments

  • Value types like booleans won't show up in the memory profiler unless they have been boxed by .NET, as in all other cases they will be part of other classes and not visible as distinct objects.

    A boxed boolean will have 8 bytes of .NET header, 1 byte of boolean and 3 bytes to make the size a multiple of 4 (exactly the same as a class containing a single boolean field). This makes them quite inefficient to store as objects - if you consider the fact that you also need to have at least one reference to this boolean the total memory required is actually 16 bytes per boolean.
    Andrew Hunter
    Software Developer
    Red Gate Software Ltd.
Sign In or Register to comment.