2007.11.16 09:29 PM

How big is a .NET System.Boolean?

A bit, a byte, two bytes? Four bytes?! Well, according to Mattias Sjögren, C# MVP, it's a single byte, and "Any MSND topics that says otherwise are wrong." So there.

This became an issue as we were calculating memory requirements for caching thousands of instances of a custom structure in arrays to avoid making redundant and expensive stock quote web service calls, and the structure included a dozen or more nullable value type members. The use of nullable value types in the structure seemed like a simple way to deal with constructing and storing a structure instance for a date for which the service returned no values, which happens for some volatile securities every once in a while. It didn't take long, though, to realize that using nullable value types for each of the structure's members added another byte for each member, as that's how the nullable generic structure works - it basically wraps the declared type with a System.Boolean to track whether it has been set. That's 12 bytes by 2,600+ struct instances by 200+ cached quote ranges - over 6MB used to accommodate a condition that occurs only very rarely. And, more importantly, a condition which, if it occurs, affects all of the member values, not just some of them. The solution, of course, was simple. Just make the structure perform like nullable<T> by reverting the members to regular value types and adding a single System.Boolean to the structure to track whether the structure's values were set.

Btw, we encountered another interesting value type size issue involving trading volumes. The aforementioned structure is used to capture closing values for both stocks and indices. To save additional space, we originally typed the trading volume members as System.Int32 (C# int, VB Integer). However, with a little research we discovered that the highest single day trading volume recorded for an index was the NYSE on January 4, 2001 when it reached 2,129,445,637 shares traded. That's just a hair short of the System.Int32 maximum value of 2,147,483,647. In this case, we figured it was better to be safe than sorry and switched the volume-related members to System.Int64 (C# long, VB Long). I suppose we could have switched to unsigned 32-bit integers instead, which would have given us headroom to 4,294,967,295, but breaking the signed-integer habit is hard to do, even if there are some good reasons to do so.


Within the managed environment

bool occupies 1 byte.

Inside BitArray, it occupies 2 bytes

When Marshalling, it takes 2 bytes.

.Net Training in Chennai | 2012.09.04 04:07 AM


When Marshalling, it takes 4 bytes.

.Net Training | 2012.09.04 04:09 AM

ToString() Converts the value of this instance to its equivalent string representation either "True" or "False".

Lev Hudson | 2013.03.18 01:14 AM


TrackBack URL:  https://www.typepad.com/services/trackback/6a00d8341c7bd453ef00e54f9abeab8834

Listed below are links to weblogs that reference How big is a .NET System.Boolean?: