This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
The reference (as opposed to the object instance) is four bytes. This should be specified somewhere in the JVM specification. In some cases it may actually take up more than four bytes - e.g. 8 bytes on a 64-bit machine - but it will still behave as if it's only got four bytes available.
I'm a little suspicious of this question I guess it might be interesting from a trivia perspective, but, unlike the C language, there is absolutely nothing you can do, programmatically, with this information. I think that Jim is right, for most JVMs, but certainly there is no guaranteed correct answer to this question. Also, I wonder if Jim's answer is less likely to be true in J2ME environments?
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Coming from other languages with a sizeOf operator or function, I missed the feature in Java. A few times it has seemed important to know things like "how big is your Session data" on a web server. But we're really gotten along quite well without knowing exact byte counts.
If you have to hunt down large objects that cause memory trouble or evaluate whether one design uses more memory than another we could talk through some tricks with garbage collector logs and such. The Java Specialist newsletter has done some work with this, too.
But, in short, the number of bytes used by an object is not something Java folks tend to need to know in detail.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi