Results 1 to 16 of 16

Thread: Memory allocation: Byte Vs Boolean

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    285

    Memory allocation: Byte Vs Boolean

    What is the difference between Memory allocation of datatype Byte and Boolean?

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Memory allocation: Byte Vs Boolean

    Have you not read both links?

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Memory allocation: Byte Vs Boolean

    If you read far enough through both links, and sub links, you will see in .Net that both are structures to support the methods associated with them, and in memory the storage for each data element is a byte.
    So, memory allocation wise, they should be the same, taking one byte per element in another class or structure.
    Of course, in use, they should be treated completely differently.

  4. #4
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Memory allocation: Byte Vs Boolean

    Unless interfacing with a legacy application, or automation/industrial system, does it matter?

    If interfacing, then rather than relying on the underlying representation of a boolean - as that's the only one that matters, here, since a byte is a byte - then you'd explicitly layout the representation. For example, a byte can represent 8 booleans, or a single boolean; if a single boolean is it 1, not zero or all 1's for true?
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Memory allocation: Byte Vs Boolean

    And if interfacing to VB6, a boolean is two bytes (based on the VB6 integer type).
    I'm assuming the reason for the question about .Net was because a byte and a boolean were different sizes in VB6.
    If a boolean was still two bytes (or more), one might choose to manually implement "booleans", by using a smaller type, a byte, or individual bits within a larger integral type, e.g. byte, short, integer, etc.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Memory allocation: Byte Vs Boolean

    I have my doubts as to whether .NET truly uses a byte for either byte or Boolean, despite what the documentation states.
    My usual boring signature: Nothing

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Memory allocation: Byte Vs Boolean

    Why not? We're only talking about the data portion of the structure.
    All the methods that pertain to the structure are a separate thing, and are shared by all objects of that structure type.
    When you call a method on the object, e.g. myBoolean.ToSTring, the ToString method of the Boolean structure is called, probably passing the byte by value that is the boolean associated with myBoolean variable to it.
    There is no reason that all the places you declare booleans or bytes can't be single bytes, as it is the compiler that generates the code that knows that byte is of type Boolean or Byte structure and can call the methods associated with that type passing the byte to them.
    Even if you choose to pass the byte or boolean by reference, then the compiler would pass the address of the byte where the boolean or byte is located. Passing the address of a byte doesn't prevent the byte from being a byte itself.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Memory allocation: Byte Vs Boolean

    Quote Originally Posted by passel View Post
    If you read far enough through both links, and sub links, you will see in .Net that both are structures to support the methods associated with them, and in memory the storage for each data element is a byte.
    So, memory allocation wise, they should be the same, taking one byte per element in another class or structure.
    Of course, in use, they should be treated completely differently.
    A Boolean in .Net takes 4 bytes of memory. You can even test this using Marshal.SizeOf against a Boolean variable to confirm.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Memory allocation: Byte Vs Boolean

    Quote Originally Posted by Niya View Post
    A Boolean in .Net takes 4 bytes of memory. You can even test this using Marshal.SizeOf against a Boolean variable to confirm.
    is this also true for a real 64bit app?

    once upon a time it was 16bits

    but as someone else said: does it really matter? if you got a huge amount of booleans to save (e.g. i had to count the number of unique colors in a 32bit rgb picture once) you are better served with manually setting bits i.e. 1 bit per boolean.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    285

    Re: Memory allocation: Byte Vs Boolean

    Quote Originally Posted by Niya View Post
    A Boolean in .Net takes 4 bytes of memory. You can even test this using Marshal.SizeOf against a Boolean variable to confirm.
    If Boolean takes 4 byte memory then we can use Byte instead of Boolean in some cases(0 or 1). Is that so?

  11. #11
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Memory allocation: Byte Vs Boolean

    What is it you are trying to do?

    Are you creating a large array of Booleans and trying to save memory by using a Type that has smaller memory consumption?

    If so, I wouldn't bother using a Byte array to hold each Boolean value, since Booleans are definitely stored as Bytes when stored in an array (and consequently in a List).

    Don't know about individual Boolean variables, but I suspect their values are stored as a Byte, but word aligned to 4 bytes (so you could store up to 4 Booleans in a 4 byte word).

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Memory allocation: Byte Vs Boolean

    For what purpose? The registers in the CPU are either 32 or 64 bits. Memory can be addressed down to the byte, in theory, but in practice it is terribly inefficient to move around a byte at a time, so that just doesn't happen even if the data type is a byte in size. Data structures may also be stored aligned to certain boundaries to improve efficiency, which means that the address is evenly divisible by some number like 4, 8, or 16. So, the memory is likely not stored at the one byte size, moved around at the one byte size, or used in the CPU registers at one byte size, so what do you expect to gain by using a byte over a Boolean?
    My usual boring signature: Nothing

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Memory allocation: Byte Vs Boolean

    Quote Originally Posted by Niya View Post
    A Boolean in .Net takes 4 bytes of memory. You can even test this using Marshal.SizeOf against a Boolean variable to confirm.
    Marshal.SizeOf is the size of the object when allocated to unmanaged memory. That is not necessarily the size the object is in managed memory.
    My understanding is that in the CLI, the boolean is only 1 byte. Compiled, the size varies.
    I'm also given the impression if you have a boolean array with a size of 16 elements, and you use Marshal.Copy to copy it to unmanaged memory, the size of the array is 16 bytes.
    Unfortunately, I don't have time to play with any of this to verify the various cases. One byte in CLI may not translate to 1 byte in the compiled executable, which is probably part of the confusion.

    Of course, you also have available the BitArray class so you can have compact storage, if that is a requirement.
    Haven't done any speed tests between the representations to measure the tradeoffs.

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Memory allocation: Byte Vs Boolean

    Quote Originally Posted by passel View Post
    Marshal.SizeOf is the size of the object when allocated to unmanaged memory. That is not necessarily the size the object is in managed memory.
    Ideally yes, but in practice, I've observed no discrepancies with a primitive type's managed and unmanaged sizes. It returns 1 for Bytes, 2 for Shorts, 4 for Integers and 8 for Longs. I see no reason why the Boolean type would be a special case. Even in VB6 a Boolean was simply a Short(Integer in VB6).
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Memory allocation: Byte Vs Boolean

    Also, the documentation states that a Boolean's storage size is platform specific. In other words, you have no guarantees that its 1 byte or 4 bytes. It happens to be 4 bytes in Windows.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  16. #16
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Memory allocation: Byte Vs Boolean

    I decided to dig a little more into this and I found a blog post by JaredPar who works at MS. He confirmed that you are indeed right passel. A Boolean is 1 byte as a managed type and its default marshalling behavior is to be sent as 4 bytes.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width