Memory size of Boolean

Code and memory profiling for .NET developers.

Memory size of Boolean

Postby palelocust » Thu Dec 18, 2008 4:52 pm

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
palelocust
 
Posts: 1
Joined: Thu Dec 18, 2008 4:49 pm

Postby AndrewH » Fri Dec 19, 2008 11:08 am

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.
AndrewH
 
Posts: 134
Joined: Thu Aug 17, 2006 3:44 pm


Return to ANTS Profiler Previous Versions

Who is online

Users browsing this forum: No registered users and 0 guests