size occupied by a byte[]

F

fabrulous

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
 
D

dsjoblom

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
 
M

Mark Space

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top