Quick revision
The short answer
On a 64-bit JVM, HotSpot normally stores references as compressed oops, 4-byte values that the JVM decodes into full addresses, and this is the default whenever the maximum heap is below about 32 GB. With a larger heap, or with a collector such as ZGC that does not support them, references are full 8-byte pointers. The JVM's ergonomics choose UseCompressedOops from the maximum heap size, and the difference matters when we estimate memory for reference-heavy structures such as HashMap or a list of objects.
What to remember
In this answer, we saw that compressed oops store references as 4-byte offsets scaled by the 8-byte object alignment, which covers heaps up to about 32 GB, and that larger heaps or collectors such as ZGC use 8-byte references. The JVM's ergonomics choose UseCompressedOops from the maximum heap size, PrintFlagsFinal shows the result, and the choice changes memory estimates for reference-heavy structures such as HashMap nodes and the backing arrays of ArrayList.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
On a 64-bit JVM, HotSpot normally stores references as compressed oops, 4-byte values that the JVM decodes into full addresses, and this is the default whenever the maximum heap is below about 32 GB. With a larger heap, or with a collector such as ZGC that does not support them, references are full 8-byte pointers. The JVM's ergonomics choose UseCompressedOops from the maximum heap size, and the difference matters when we estimate memory for reference-heavy structures such as HashMap or a list of objects.
Why this question is asked
Most developers know that a 64-bit machine uses 8-byte pointers, so it is surprising to learn that a reference field in Java usually takes only 4 bytes. The difference is big enough to change memory estimates by a third or more, and it explains why some services use more memory after a heap increase than before. Interviewers ask this to check that we can reason about object sizes on the real JVM. In this answer, we will see what an oop is and how compression works, when the JVM uses 8 bytes instead, how to check which mode we are in, and what it means for collection sizes.
1What an Oop Is and How It Is Compressed
An oop, short for ordinary object pointer, is HotSpot's name for a reference to an object on the heap. On a 64-bit machine a raw address is 8 bytes. But HotSpot aligns every object to 8 bytes, so the lowest three bits of every object address are always zero and carry no information.
Compressed oops use that fact. Instead of the full address, the JVM stores the object's offset from the start of the heap divided by 8, in 32 bits. To use the reference, it multiplies by 8 (a shift by 3) and adds the heap base. 32 bits can count 4 billion units of 8 bytes, which is 32 GB, so any heap up to about 32 GB can be addressed with 4-byte references.
2When References Are 8 Bytes
If the maximum heap is 32 GB or more, a 32-bit offset can no longer reach every object, so the JVM switches compressed oops off and uses plain 8-byte addresses. The limit is slightly below 32 GB in practice, which is why people use -Xmx31g when they want a large heap that keeps 4-byte references.
Some collectors do not use compressed oops at all. ZGC stores extra information in its pointers, so its references are always 8 bytes, whatever the heap size. G1, Parallel and Serial use compressed oops for heaps below the limit, and so does Shenandoah.
| Setup | UseCompressedOops | Reference size |
|---|---|---|
| G1, -Xmx4g | true (ergonomic) | 4 bytes |
| G1, -Xmx31g | true (ergonomic) | 4 bytes |
| G1, -Xmx32g or more | false (ergonomic) | 8 bytes |
| ZGC, any heap size | false | 8 bytes |
-XX:-UseCompressedOops, any heap | false (command line) | 8 bytes |
3How the JVM Decides, and How to Check
We rarely set UseCompressedOops ourselves. The JVM's ergonomics, the logic that picks defaults at startup, look at the maximum heap size and the collector and turn compressed oops on if they can work. Because the maximum heap in a container is often derived from the memory limit, raising a pod's limit can silently cross the boundary.
4Why It Matters for HashMap and ArrayList
Reference-heavy structures double their pointer cost when references are 8 bytes. An ArrayList of one million objects holds an internal Object[] of at least one million slots: 4 MB with compressed oops and 8 MB without, before counting the objects themselves. A HashMap entry, the Node object, holds a hash and three references, key, value and next.
Notice that the header stays 12 bytes in both cases, because compressed class pointers are independent of compressed oops in current JDKs. The growth comes entirely from the reference fields. For estimates, we pick the reference size that matches the real heap and collector, and verify with JOL when it matters.
5Follow-up questions
Is decoding a compressed reference slow?
It costs a shift and sometimes an add, which modern CPUs do almost for free, and when the heap is placed low in memory the JVM can even skip the add. In return, objects are smaller, more of them fit in CPU caches, and the GC has less memory to scan, so compressed oops usually make programs faster, not slower.
Can I keep compressed oops with a 40 GB heap?
Only by changing the alignment. With -XX:ObjectAlignmentInBytes=16, the shift becomes 4 and 32 bits can address 64 GB, but every object is then padded to a multiple of 16 bytes, which wastes space. It is rarely worth it; staying just under 32 GB or using a much larger heap is the usual advice.
Are compressed class pointers the same thing?
No. Compressed class pointers shrink the class pointer in every object header to 4 bytes. They use a separate compressed class space and stay on even when compressed oops are off.
6Conclusion
In this answer, we saw that compressed oops store references as 4-byte offsets scaled by the 8-byte object alignment, which covers heaps up to about 32 GB, and that larger heaps or collectors such as ZGC use 8-byte references. The JVM's ergonomics choose UseCompressedOops from the maximum heap size, PrintFlagsFinal shows the result, and the choice changes memory estimates for reference-heavy structures such as HashMap nodes and the backing arrays of ArrayList.
Open with
On a 64-bit JVM a raw address is 8 bytes, but HotSpot usually stores references as compressed oops, which are 4 bytes. It works because every object is aligned to 8 bytes, so the last three bits of an address are always zero.
Then, if they want more
The JVM stores the offset from the heap base divided by 8 in 32 bits, and decodes it with a shift and an add. 32 bits of 8-byte units cover 32 GB, so compressed oops are the default for heaps below about 32 GB.
Above that, the JVM switches to full 8-byte references, and some collectors, like ZGC, always use 8-byte references regardless of heap size. The setting is UseCompressedOops, but normally nobody sets it: ergonomics choose it at startup from the maximum heap size and the collector, and I can check the result with PrintFlagsFinal. It matters for memory estimates.
A HashMap node with a hash and three references is 32 bytes with compressed oops and 40 bytes without, and the backing array of an ArrayList of a million objects is 4 MB versus 8 MB. The header stays 12 bytes either way, because compressed class pointers are separate.
So when I estimate the size of a structure, I first find out which mode the real JVM runs in.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why does a reference field take 4 bytes on most 64-bit JVM heaps but 8 bytes on some?...": https://scrivio-interview-library.netlify.app/java/memory-layout/5e71244672/