size occupied by a byte[]

Discussion in 'Java' started by fabrulous@yahoo.fr, Jul 7, 2006.

  1. Guest

    Hi everybody,

    I seem to remember that there where some JVM that would "waste" space
    when allocating a byte[]. But I'm too sure about that anymore, so I've
    got a few questions...

    Does a boolean[] waste space? And is this defined in some
    specification?

    Does an byte[] waste space? And is this defined in some specification?

    How can I find this by myself by "hacking it"? (was thinking of
    running that in a virtual machine and examine the memory by placing
    some pattern in the byte[] but this seems very stupid).

    :)

    fab
     
    , Jul 7, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hi everybody,
    >
    > I seem to remember that there where some JVM that would "waste" space
    > when allocating a byte[]. But I'm too sure about that anymore, so I've
    > got a few questions...
    >
    > Does a boolean[] waste space? And is this defined in some
    > specification?


    You may be thinking of the JVM specification. There are no special
    bytecode instructions for manipulating booleans. The instructions for
    reading/writing from/to boolean and byte arrays are the same. And in
    early Sun VMs a boolean array did indeed use 8 bits per element. This
    is however not mandated by the spec, a VM could very well use packed
    arrays, but I /think/ most VMs will go with bytesized booleans. The
    downside of packed arrays is that while you save some space, array
    access becomes quite expensive. If you are worried about the extra
    space use of boolean arrays, try using java.util.BitSet instead which
    is almost guaranteed to use a packed int/long implementation.

    It is not likely that a JVM would waste any space when allocating a
    byte array, since bytes are readily accesible on just about any
    processor, as opposed to single bits which require extra code to
    access.

    Regards,
    Daniel Sjöblom
     
    , Jul 7, 2006
    #2
    1. Advertising

  3. Mark Space Guest

    wrote:
    > wrote:
    >> Hi everybody,
    >>
    >> I seem to remember that there where some JVM that would "waste" space
    >> when allocating a byte[]. But I'm too sure about that anymore, so I've
    >> got a few questions...
    >>
    >> Does a boolean[] waste space? And is this defined in some
    >> specification?

    >
    > It is not likely that a JVM would waste any space when allocating a
    > byte array, since bytes are readily accesible on just about any
    > processor, as opposed to single bits which require extra code to
    > access.


    Well, any object in Java has about 8 bytes of overhead, at least. Do a
    google search for "java memory object footprint," or similar, to get
    some ideas. Primitives are NOT objects and don't have this issue. But
    a single boolean could still take as much as 8 bytes, depending on the
    JVM (esp. on 64 bit systems).

    The best way to determine memory overhead is to get some of the test
    programs that the google search above will give you, and try them out.
     
    Mark Space, Jul 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravi

    Show all memory occupied by the program

    Ravi, Jun 3, 2007, in forum: C Programming
    Replies:
    31
    Views:
    821
    Keith Thompson
    Jun 6, 2007
  2. Lambda
    Replies:
    2
    Views:
    2,784
    James Kanze
    May 20, 2008
  3. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,661
    Michael Jung
    May 25, 2008
  4. Replies:
    5
    Views:
    369
    Jerry Coffin
    Jun 15, 2008
  5. swapnalimore
    Replies:
    0
    Views:
    703
    swapnalimore
    Jul 12, 2011
Loading...

Share This Page