GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes size of reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "size of reference" Watch "size of reference" New topic
Author

size of reference

amar nath jha
Ranch Hand

Joined: Mar 09, 2006
Posts: 42
Is there any way to know the size of reference in java?

example:

Light light = new Light();

can we know the size of light .
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
have a look at

campfire stories


The Zen of Java Programming.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 not back." - Bill Harding, Twister
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hi Guys,

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.)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
 
GeeCON Prague 2014
 
subject: size of reference