views:

94

answers:

2

A reference to an Object on a 32 bit JVM (at least on Hotspot) takes up 4 bytes.

Does the 64 bit Hotspot JVM need 8 bytes? Or is some clever compression going on? If not, every Object[] would require twice as much heap memory, which I somehow think (hope, expect) is not the case.

Update/extra question: Does this really matter, or is this a negligible increase, because most references point to objects that are much larger than a few bytes (whereas one might argue that those objects are in turn mostly comprised of references to other objects)?

+3  A: 

According to Java Platform Performance it is not strictly defined, but typically 8 bytes on a 64-bit system:

The size of a reference isn't well defined, but it is typically 4 bytes on a 32-bit system and 8 bytes on a 64-bit system.

aioobe
So how does Hotspot handle this? If every reference is 8 bytes, that would mean a much higher heap requirement.
Thilo
Beats me. Haven't thought of it. Isn't it negligible, though? Usually, each object stores *something* which most of the time exceeds those 8 bytes. Sure, for small objects I can see that it could be an issue, but perhaps one would be better of with a primitive in such cases.
aioobe
The book has a section (on the same page) *Measuring Object Size* describing how to measure sizes of objects. Perhaps you could give it a try and report back.
aioobe
Well, most of the things my objects store are pointers to other objects, so those would double, too. The only thing that does not grow would be primitives and things like String (which is basically a char[])
Thilo
+7  A: 

In a 64-bit system, object references are typically 8-byte long. But in recent JVMs from Sun/Oracle you can enable Compressed Oops, which reduce reference size to 4 bytes at the cost of a smaller limit on heap size.

gpeche
+1, interesting!
aioobe