17

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)?

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    You might find this helpful - http://stackoverflow.com/questions/1443677/what-impact-if-any-does-the-d64-swtich-have-on-sun-jvm-resident-memory-usage – Vineet Reynolds Sep 17 '10 at 07:18
  • And this one: http://stackoverflow.com/questions/783662/java-32-bit-vs-64-bit-compatibility – Thilo Sep 17 '10 at 07:34
  • 1
    Just to give a data point, when we switched our webapp from 32-bit to 64-bit JVM, heap usage went up by ~30%. Your mileage may vary, but that at least gives you a ballpark. – Cowan Sep 17 '10 at 21:32

2 Answers2

16

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.

Cripto
  • 3,581
  • 7
  • 41
  • 65
gpeche
  • 21,974
  • 5
  • 38
  • 51
  • I think it is enabled by default for java 7 or >java6 u23, right ? – Rajat Gupta Aug 04 '13 at 09:35
  • "Compressed oops is supported and enabled by default in Java SE 6u23 and later releases." See: [Java SE 6 Update 23 Release notes](http://www.oracle.com/technetwork/java/javase/6u23releasenotes-191058.html) – snorbi Sep 06 '13 at 18:00
  • But: "In Java SE 7, use of compressed oops is the default for 64-bit JVM processes when -Xmx isn't specified and for values of -Xmx less than 32 gigabytes. For JDK 6 before the 6u23 release, use the -XX:+UseCompressedOops flag with the java command to enable the feature." See http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html#compressedOop – Andy Thomas Mar 19 '14 at 17:40
  • @gpeche you wrote "you can enable Compressed Oops, which reduce reference size to 4 bytes at the cost of a smaller limit on heap size." . Why would Compressed oops cause a smaller limit on heap size? – Geek Apr 21 '15 at 06:38
  • 1
    @Geek since you can address less memory with 32 bit addresses than with 64bit addresses. – Ingo Feb 15 '16 at 18:57
6

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
  • 413,195
  • 112
  • 811
  • 826
  • So how does Hotspot handle this? If every reference is 8 bytes, that would mean a much higher heap requirement. – Thilo Sep 17 '10 at 06:57
  • 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 Sep 17 '10 at 07:00
  • 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 Sep 17 '10 at 07:04
  • 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 Sep 17 '10 at 07:05