Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

size occupied by a byte[]

Thread view: 
fabrulous@yahoo.fr - 07 Jul 2006 05:03 GMT
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
dsjoblom@abo.fi - 07 Jul 2006 15:03 GMT
> Hi everybody,
>
[quoted text clipped - 4 lines]
> 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
Mark Space - 07 Jul 2006 18:11 GMT
>> Hi everybody,
>>
[quoted text clipped - 9 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.