memory usage of byte ?
Peter B. Kessler
Peter.Kessler at Sun.COM
Sat Jul 5 13:26:20 PDT 2008
Ulf Zibis wrote:
> Hi all,
> can you tell me something about memory consuming in the current Sun JVM?
> How much bytes do byte arrays consume per byte?
> ... or in other words: is it less memory-consuming using byte arrays
> than int arrays, if these are large?
> Same question about short and char arrays?
> Thanks for a short answer.
> -- please CC to my email.
Every object in the HotSpot JVM has a 2-word header, where the
word size is 32-bits in the 32-bit JVM and 64-bits in the 64-bit
JVM (duh). An array then has a word that holds the length of
the array. Following that comes the data, in whatever size is
appropriate: boolean and byte elements take 1 byte each, chars
and shorts take 2 bytes, ints and floats take 4 bytes, and longs
and doubles take 8 bytes. References to other objects take either
4 or 8 bytes depending on whether you are in a 32-bit JVM or a
64-bit one (with a twist with compressed oops). If the data needs
to be aligned (e.g., doubles), then padding is inserted between
the array length and the data if needed. After the data there's
enough bytes of padding to get us to an 8-byte boundary (so that
all objects start on an 8-byte boundary).
So in answer to your direct questions: byte arrays are less
memory-consuming than int arrays, especially if the arrays are
large. Short and char arrays are the same size, though there
are published schemes to squeeze char arrays if you aren't using
the high-order bytes. We don't use any of those, yet. (But you
are welcome to contribute to that implementation if you think
it's important. The trick is not slowing down the people that
aren't using it.)
You can use -XX:+PrintClassHistgram on the java command line and
"kill -QUIT" (from another shell) to see how big things are.
A more user-friendly way of browsing the heap of a Java program
is "jmap -dump" followed by "jhat". See the manual pages for
those tools to see how to use them.
More information about the hotspot-dev